Mubasshir Pawle
Mubasshir Pawle

Reputation: 319

CakePHP 2.3.0 component $controller is not accessible

I am using Cakephp 2.3.0, loading following component.

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);
}

}

It get error following error

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

I am unable to access $this->controller there. Any reason? How do I make it work?

Upvotes: 1

Views: 2365

Answers (1)

Rikesh
Rikesh

Reputation: 26421

Read here startup method is called after the controller so need to initialize controller in initialize method as below,

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

Upvotes: 4

Related Questions