1

我正在尝试在模块中获取工作布局。所以我在名为“adminLayout”的模块的视图文件夹中创建了一个布局

假设AdminModule.phpinit() 方法中的布局。所以现在它看起来像这样:

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.*',
    ));


}

但由于某种原因布局不适用于模块。我尝试将“public $layout”添加到控制器并且它有效。

无法弄清楚是什么问题。

我也尝试将布局设置添加到main.php配置文件夹中,但仍然没有任何操作。如果有人可以提供帮助,将不胜感激。

4

3 回答 3

4

解决方案是在模块中的 beforeControllerAction 上设置布局。它应该工作。

 public function beforeControllerAction($controller, $action)
  {
    if(parent::beforeControllerAction($controller, $action))
    {
      $controller->layout = 'adminLayout';
      return true;
    }
    else
      return false;
  }
于 2013-07-26T12:31:12.427 回答
2

关于这个主题有很多帖子,答案在 Yii 文档中:

布局属性

公共混合$布局;

此模块内的控制器共享的布局。如果控制器显式声明了自己的布局,则此属性将被忽略。如果这是 null(默认),则将使用应用程序的布局或父模块的布局(如果可用)。如果这是错误的,那么将不使用任何布局。

只需从控制器中检测模块并相应地设置布局:

class Controller extends CController
{

public function init(){

    //Set layout
    $this->layout = ($this->module->id=='admin') ? '//layouts/column2' : '//layouts/column1';
.........
}
于 2014-09-17T09:50:17.333 回答
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" />
于 2013-07-26T16:19:48.507 回答