2013-10-03 39 views
0

我嘗試了幾乎所有的東西,但我無法獲得以下內容來運行。無法通過構造函數設置佈局

<?php 

class BaseController extends Controller { 

    // Define frontpage layout manager 
    protected $layout = ''; 

    public function __construct() { 
    parent::__construct(); 

    $theme = Theme::where('enabled', '=', true)->first(); 

    // HERE !! : This never changes the value of $layout class var 
    $this->layout = View::make('themes.front.' . $theme->folder . '.master'); 
    // I also tried without View::make(..) 
    // I also checked that $theme returns something and it does return a theme 


    } 

    /** 
    * Setup the layout used by the controller. 
    * 
    * @return void 
    */ 
    protected function setupLayout() 
    { 
    if (! is_null($this->layout)) 
    { 
     $this->layout = View::make($this->layout); 
    } 
    } 

} 

我根本無法在我的構造函數中更改$ layout的值。我需要這個讓用戶在佈局之間切換。

+0

有沒有錯誤?它應該沒有'View :: make'。只有'$ this-> layout ='themes.front。' 。 $ theme->文件夾。 '的.master';' –

回答

0

所以我想實現這個是:我會有多個佈局(模板),我會允許用戶通過管理更改這些模板,所以我需要一個快速和簡單的方法來操縱protected $layout值。

把我的代碼放在__constructor() {}的問題是setupLayout()會覆蓋它,因此找不到佈局的錯誤。

因此,有兩種解決方案:

1)在每個子控制器
聲明佈局意味着每個擴展你的基地控制器定義它自己在它自己的__constructor() {}方法protected $layout控制器。但是,如果您的所有頁面共享相同的模板,則這是非常重複的。

2)Maniuplate的setupLayout()方法
因爲我所有的頁面共享相同的佈局,我知道certian總會有至少一個模板,我能夠簡單地改變setupLayout()方法:

function setupLayout() 
{ 
    $theme = Theme::where('enabled', '=', true)->first(); 
    $this->layout = 'themes.front.' . $theme->folder . '.master'; 
}