CakePHP 2.3.0组件$ controller无法访问

时间:2013-05-20 08:41:00

标签: cakephp components cakephp-2.0

我正在使用Cakephp 2.3.0,加载以下组件。

class BreadCrumbsComponent extends Component {

public $components = array();
public $controller = null;

public function initialize($controller) {

}

public function startup($controller) {
    $this->controller = $controller;
}

public function beforeRender($controller) {

}

public function shutDown($controller) {

}

public function beforeRedirect($controller, $url, $status = null, $exit = true) {

}

public function handle($controllerName = NULL, $actionName = NULL) {
    pr($this->controller->modelClass);
}

}

错误跟踪错误

Trying to get property of non-object [APP\Controller\Component\BreadCrumbsComponent.php, line 38]

我无法在那里访问$ this->控制器。任何原因?我如何使它工作?

1 个答案:

答案 0 :(得分:4)

在控制器之后调用here 启动方法,因此需要在 initialize 方法中初始化控制器,如下所示,

public function initialize(&$controller, $settings = array()) {
    $this->controller = $controller;
}