控制器 cakephp 2 中的全局变量 [英] Global variable in controller cakephp 2
本文介绍了控制器 cakephp 2 中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在控制器中使用全局变量的方法是什么?
What's the way to have a global variable inside a controller?
我曾尝试使用 beforeFilter 来做到这一点,但其他函数无法访问它.
I have tried to do it using beforeFilter but it is not accessible from the others functions.
是否只能使用Configure::read
和Configure::write
推荐答案
您可以在 AppController 中的任何控制器中设置可访问的变量
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屋!
查看全文