Global variable in controller cakephp 2

泄露秘密 提交于 2019-12-01 08:10:39

you can set variable accessible in any controller in your AppController

class AppController extends Controller {
    public $myGlobalVar;  

    public function beforeFilter()
    {
         //this can be anything array, object, string, etc .....
         $this->myGlobalVar = "test2";
    }
 }

then in your other controller you can access variable anywhere like this

class TestController extends AppController {

    public function index() {

        debug($this->myGlobalVar);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!