2014-11-02 62 views
0

有沒有辦法處理Laravel中特定控制器的'before make view'事件? 我的意思是在生成一個視圖之前,我想爲特定控制器的每個動作做一個共同的事情,使之乾燥。 謝謝。Laravel在查看事件之前

+0

它是什麼,你想爲每個動作做? – lukasgeiter 2014-11-02 12:01:54

+0

我想做一個 - >('sectionTitle',$ this-> sectionTitle); * $ sectionTitle是我的後端應用程序中一個節的頁面標題,它從我的控制器的動作變爲動作 – 2014-11-02 12:11:38

回答

0

您可以使用setupLayout方法。如果您正在使用默認laravel安裝附帶的BaseController類。它已被定義,您可以擴展或覆蓋它。

BaseController(如果你使用它,想做到這一點從它延伸的每個控制器)

protected function setupLayout() 
{ 
    if (! is_null($this->layout)) 
    { 
     $this->layout = View::make($this->layout); 
    } 

    if(isset($this->sectionTitle)){ // just to be sure 
     View::share('sectionTitle', $this->sectionTitle); 
    } 
} 

你的一個控制器的

protected function setupLayout() 
{  
    if(isset($this->sectionTitle)){ // just to be sure 
     View::share('sectionTitle', $this->sectionTitle); 
    } 
    parent::setupLayout(); // add this if you're extending from a controller that has implemented the setupLayout() function 
} 
+0

這就是我一直在尋找的,謝謝 – 2014-11-02 14:37:24