在 Controller 中拥有全局变量的方法是什么?
我曾尝试使用 beforeFilter 来做到这一点,但无法从其他函数访问它。
只能使用 Configure::read
来完成吗?和 Configure::write
最佳答案
您可以在 AppController 中的任何 Controller 中设置可访问的变量
class AppController extends Controller {
public $myGlobalVar;
public function beforeFilter()
{
//this can be anything array, object, string, etc .....
$this->myGlobalVar = "test2";
}
}
然后在您的其他 Controller 中,您可以像这样在任何地方访问变量
class TestController extends AppController {
public function index() {
debug($this->myGlobalVar);
}
}
关于cakephp - Controller cakephp 2 中的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12638962/