2013-02-08 28 views
0

我想加載幫助程序取決於佈局。 我想是這樣的,但ID doesnt't工作:cakephp 2 - 根據佈局加載幫助程序

function beforeRender(){ 
    if (array_key_exists($this->request->action, $this->custom_layouts)){ 
    public $helpers = array('Html', 'Form', 'Session', 'Menu1');     
    $this->layout = $this->custom_layouts[$this->action]; 

    }else{ 
    public $helpers = array(
     'Session', 
     'Html' => array('className' => 'TwitterBootstrap.BootstrapHtml'), 
     'Form' => array('className' => 'TwitterBootstrap.BootstrapForm'), 
     'Paginator' => array('className' => 'TwitterBootstrap.BootstrapPaginator'), 
     'Menu1' 
    ); 
    $this->layout = 'default'; 
    } 
} 

感謝您的幫助 問候

回答

2

你不能JUSE聲明public $helpers內的方法。

您需要爲PHP希望它被調用來調用它:

$this->helpers[] = 'Menu1'; 
$this->helpers['Html'] = array('className' => 'TwitterBootstrap.BootstrapHtml'); 
... 
+0

請測試是否動態使用不同的傭工相同的別名(例如,有時$這個 - > HTML映射到「BootstrapHtmlHelper」有時到'HtmlHelper')按預期工作。爲不同的類指定相同的別名可能會導致Cake緩存出現問題,並可能導致意外的結果(例如,隨機附加'錯誤'類型的對象)。至少,'Models'就是這種情況,但同樣的*可能*適用於其他課程 – thaJeztah 2013-02-08 21:04:01

+0

非常感謝你的標記,作品完美。到目前爲止,它正常工作 – mart 2013-02-08 21:23:03

+0

@馬丁很好聽,只要確保測試與調試設置爲0,因爲只有然後緩存完全啓用。 – thaJeztah 2013-02-08 21:35:52