AppController 中设置的 CakePHP 变量在一个控制器中可见,在另一个控制器中不可见

CakePHP variable set in AppController is visible in one controller and not visible in another

在 cakephp 2.0 中,我试图在 AppController 中设置一个变量,以便可以通过以下方式在任何子 classes 和视图中访问它:

function beforeFilter() {       
    $this->set('lang', self::getCurrentLanguage()); // set to access it also from Views
    $this->set('conf', self::getConfig());          // set to access it also from Views     
    $this->lang = self::getCurrentLanguage();   // set to access it also from Controllers
    $this->conf = self::getConfig();            // set to access it also from Controllers
    $this->set('user', $this->Session->read('User'));
    $this->user = $this->Session->read('User');     
}

当我使用 echo $lang 时,一切运行顺利;在 View 中并在任何控制器中回显 $this->lang,除了具有其各自 View 的控制器:

echo $conf;
echo $lang;
---
Notice (8): Undefined variable: conf [APP\views\admin\products.ctp, line 7]
Notice (8): Undefined variable: lang [APP\views\admin\products.ctp, line 8]

由于 class AdminController 扩展了 AppController,我期待与 AppController 的任何其他子 class 具有相同的行为。已经花了半天时间才找到问题所在。调查的起点在哪里?我应该先检查什么?

AdminController 有什么问题,因为它 "see" 没有变量,但其他 class 没有这个问题? 这是唯一适用于此特定 class:

的解决方法
$this->set('conf', parent::getConfig());

检查您是否定义了AdminController::beforeFilter()而忘记调用AppController::beforeFilter()

AdminController中:

public function beforeFilter() {
    parent::beforeFilter();
}

食谱中的 App Controller 部分警告您:

Also remember to call AppController‘s callbacks within child controller callbacks for best results: