I have variables in each controller, and I have been passing them to the view with $this->set for every function. It's repetitive. How can I simply refer to the ob开发者_Python百科ject that renders the view and thus refer to its variable?
Use your beforeFilter or beforeRender. So if you have the same set of variables you need to set for every function, just add this to the top of your controller:
function beforeRender() {
parent::beforeRender();
$this->set('var_name1', $this->my_var1);
$this->set('var_name2', $this->my_var2);
}
UPDATE: Thanks for bringing that to my attention. You are correct. I added $this
to the variables.
精彩评论