控制器中的全局变量cakephp 2 [英] Global variable in controller cakephp 2
本文介绍了控制器中的全局变量cakephp 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在控制器中有一个全局变量的方法是什么?
我试图使用beforeFilter,但是不能从其他函数访问。 p>
只能使用 Configure :: read
和 Configure :: write $您可以在AppController的任何控制器中设置变量
$ b code>
解决方案
$ b
类AppController extends Controller {
public $ myGlobalVar;
public function beforeFilter()
{
//这可以是任何数组,对象,字符串等.....
$ this-> myGlobalVar =test2;
}
}
然后在其他控制器中,
class TestController extends AppController {
public function index(){
debug($ this-> myGlobalVar);
}
}
What's the way to have a global variable inside a controller?
I have tried to do it using beforeFilter but it is not accessible from the others functions.
Can it only be done using Configure::read
and Configure::write
解决方案
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);
}
}
这篇关于控制器中的全局变量cakephp 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文