Yii模块布局 [英] Yii module layout

查看:40
本文介绍了Yii模块布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在模块中获得工作布局.因此,我在名为'adminLayout'

I am trying to get work layout in a module. So I created a layout in view folder of module called 'adminLayout'

假定init()方法中AdminModule.php中的布局. 所以现在看起来像这样:

Assuming a layout in AdminModule.php in init() method. So now it looks like this:

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"添加到控制器,并且可以正常工作.

But for some reason layout didn't apply to module. I tried to add "public $layout" to controller and it works.

不知道是什么问题.

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

Also I tried to add layout settings to main.php in config folder, but still no action. Will be grateful if someone could help.

推荐答案

解决方案是在模块中的beforeControllerAction上设置布局.应该可以.

The solution is set the layout on beforeControllerAction in your module. It should work.

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

这篇关于Yii模块布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆