6

I need to set a variable in the app_controller of CakePHP and then use it in my default layout file.

Is there a way to set this variable?

3 Answers 3

11

I think what he meant was, that he doesn't know where to set a variable since he's not in a specific function inside a controller. To have a variable (or anything else really) available everywhere, you have to put it in your AppController like this:

function beforeFilter()
  {
  $this->set('whatever', $whatever);
  }

More on those callback functions here.

Sign up to request clarification or add additional context in comments.

Comments

5

The callback functions in AppController are the place to $this->set() variables that you want available to all of your views and layouts. beforeFilter() is called before all controller actions. If you want to set a view variable after an action has run, use beforeRender(). You can access your other view variables in the $this->viewVars associative array.

function beforeRender() {
    $new = "Universal " . $this->viewVars['layoutTitle']; 
    $this->set('universalTitle', $new);
}

Comments

0

You can use this to set the variable : $this->set(compact('currentJobId'));

It will set array at the same time it will set values to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.