开发者

cakephp how refer to object calling/rendering a view

开发者 https://www.devze.com 2023-02-28 19:53 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号