2013-07-26 110 views
1

我正在嘗試在模塊中獲取工作佈局。因此,我在模塊的視圖文件夾中創建了一個佈局,名爲'adminLayout'Yii模塊佈局

假設init()方法中的AdminModule.php中的佈局。 所以現在看起來是這樣的:

public function init() 
{ 

    $this->layoutPath = Yii::getPathOfAlias('application.modules.admin.views.layouts'); 
    $this->layout = 'adminLayout'; 
    // this method is called when the module is being created 
    // you may place code here to customize the module or the application 

    // import the module-level models and components 
    $this->setImport(array(
     'admin.models.*', 
     'admin.components.*', 
    )); 


} 

但由於某些原因的佈局並不適用於模塊。我試圖添加「公共$佈局」控制器,它的工作原理。

找不出什麼問題。

另外我試圖在config文件夾中添加布局設置到main.php,但仍然沒有任何操作。如果有人能幫忙,請多多指教。

回答

4

該解決方案在您的模塊中設置beforeControllerAction的佈局。它應該工作。

public function beforeControllerAction($controller, $action) 
    { 
    if(parent::beforeControllerAction($controller, $action)) 
    { 
     $controller->layout = 'adminLayout'; 
     return true; 
    } 
    else 
     return false; 
    } 
+0

非常感謝你 – timofeiMih

0

你的模塊中創建資產文件夾。添加以下代碼爲assetsURL

private $_assetsUrl; 

public function getAssetsUrl() 
{ 
    if ($this->_assetsUrl === null) 
     $this->_assetsUrl = Yii::app()->getAssetManager()->publish(
      Yii::getPathOfAlias('admin.assets')); 
    return $this->_assetsUrl; 
} 

創建beforeControllerAction功能,並添加$controller->layout

public function beforeControllerAction($controller, $action) 
{ 
    if(parent::beforeControllerAction($controller, $action)) 
    { 
     // this overwrites everything in the controller 
     $controller->layout = 'adminLayout'; 
     // this method is called before any module controller action is performed 

     return true; 
    } 
    else 
     return false; 
} 

導入所有CSSJS文件如:

<link rel="stylesheet" type="text/css" href="<?php echo $this->module->assetsUrl; ?>/css/style.default.css" media="screen, projection" /> 
1

有很多帖子關於這個問題,答案在Yii文檔中:

佈局屬性

public mixed $ layout;

該模塊內控制器共享的佈局。 如果控制器已經明確聲明瞭自己的佈局,則該屬性將被忽略。如果這是空(默認),則將使用應用程序的佈局或父模塊的佈局(如果可用)。如果這是錯誤的,那麼將不使用佈局。

只要從控制器內檢測模塊,並相應地設置佈局:

class Controller extends CController 
{ 

public function init(){ 

    //Set layout 
    $this->layout = ($this->module->id=='admin') ? '//layouts/column2' : '//layouts/column1'; 
......... 
}