2013-05-20 28 views
1

我使用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] 

我無法訪問$這個 - >控制器存在。任何原因?我如何使它工作?

+0

你驗證啓動()被觸發?它應該是,如果你正確地啓動你的組件,正如文檔解釋它。所以,因爲這很可能是你的問題,你應該在這裏發佈相關的控制器代碼。 – mark

+1

試着用'initialize'方法初始化你的控制器。 – Rikesh

+0

@mark yes啓動()正在工作 –

回答

4

here啓動方法被調用後,控制器等都需要在初始化控制器初始化方法如下,

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

startup()通常也起作用 - 如果handle()方法不用於beforeFilter方法,而是用在action本身。看來,這不是這種情況。所以你的解決方案是最好的:) – mark

+0

@mark - 是的,你是對的。 – Rikesh