在控制器中使用全局变量的方法是什么?
我尝试使用 beforeFilter 来执行此操作,但无法从其他函数访问它。
只能使用来完成吗Configure::read
and Configure::write
您可以在 AppController 中的任何控制器中设置可访问的变量
class AppController extends Controller {
public $myGlobalVar;
public function beforeFilter()
{
//this can be anything array, object, string, etc .....
$this->myGlobalVar = "test2";
}
}
然后在你的另一个控制器中你可以像这样访问变量
class TestController extends AppController {
public function index() {
debug($this->myGlobalVar);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)