2012-10-14 25 views
2

我在我的web項目中使用Yii框架。現在,我有幾個模塊,我只想爲所有模塊使用一個佈局。我已經使用以下代碼用於確定每個模塊中的每個控制器/動作的佈局:在Yii中使用幾個模塊的通用佈局

$this->layoutPath = Yii::getPathOfAlias('application.views.layouts'); 
$this->layout = '//layouts/myLayout'; 

是否有任何其他的解決方案,通過使用相同的碼在初始化每個模塊的()函數來做到這一點? 換句話說,我必須在每個動作中編寫上面的2行代碼,我認爲它不好,我想減少我的代碼行數。例如如下:

class StaffModule extends CWebModule 
{ 
    public $layout; 
    public $layoutPath; 
    public function init() 
    { 
     $this->layoutPath = Yii::getPathOfAlias('application.views.layouts'); 
       $this->layout = '//layouts/myLayout'; 
     $this->setImport(array(
      'staff.models.*', 
      'staff.components.*', 
     )); 
    } 
} 

但它不起作用。請幫幫我。

回答

0

您在初始化函數是具有做法是正確的direction..I想問題可能是..因爲你定義layoutPath你不應該有//layouts..

$this->layoutPath = Yii::getPathOfAlias('application.views.layouts'); 
$this->layout = 'myLayout'; 

,而你沒有需要這些:

public $layout; 
public $layoutPath; 
2

只需使用

$這 - >佈局= '//佈局/ myLayout';

而不

$這 - > layoutPath = Yii的:: getPathOfAlias( 'application.views.layouts');

因爲//意味着你從根本

特定的絕對路徑