2013-05-07 54 views
0

如何在組件存在的情況下首先添加包含檢查的組件。 我用這個介紹CakePHP 2.2.3在cakephp中添加組件2.3.4

public function __construct($request = null, $response = null) { 
    parent::__construct($request, $response); 
    $this->_setupApplicationComponents(); 
} 

protected function _setupApplicationComponents() { 
if (App::import('Component', 'Search.Prg')) { 
    $this->components[] = 'Search.Prg'; 
} 
} 

它不是在CakePHP的2.3.4工作。
任何人都可以提供幫助。
謝謝

回答

1

我不太確定爲什麼在使用它之前檢查組件是否存在;如果找不到組件,CakePHP會自動產生錯誤?

CakePHP 2.3使用'延遲加載',這意味着組件實際上並沒有被加載/構建,直到它被實際使用。這意味着更少的開銷,並且會使您的應用程序工作更快。

若要指示您可以使用某個類(組件),請使用App::uses();見Loading Classes

在你的情況,加載從Search插件Prg組件;

App::uses('Prg', 'Search.Controller/Component'); 

但是,要使用一個組件,只需將其添加到$components陣列控制器你的,和CakePHP應自動處理它;

public $components = array(
    // Pluginname.Componentname 
    'Search.Prg', 
); 

Using Components

+0

其實組件不是懶加載,只有模型(至少在2.6.3)。放置在$ components中的每個組件都被初始化。 – 2015-03-17 16:18:44