yii模块布局自定义

时间:2013-10-17 09:49:42

标签: php layout yii module

在yii中,我创建了一个名为“admin”的模块,我想将模块设置为使用我自定义的布局而不是defalult.so我搜索了一些帖子并且它有效!  这是我读的帖子:

http://www.yiiframework.com/forum/index.php/topic/24767-module-layout/

在我将我的应用配置为帖子后,我的应用程序运行得很好。但现在我有一些问题:

  1. layoutPath
    帖子说配置
    $this->layoutPath = Yii::getPathOfAlias('administration.views.layouts');
    layoutPah变量让我非常困惑,似乎这个变量没有在任何类中定义。为什么它可以被$this调用?
  2. /layouts/main
    /layouts/main//layouts/main之间有什么区别 当使用//layouts/main它不起作用时,应用仍会调用默认的主要布局而不是我的?
  3. 重写
    该帖子还说控制器中的public $layout='//layouts/column1';覆盖了 在模块控制器的init()方法中设置的值。 这是真的吗?

2 个答案:

答案 0 :(得分:2)

  1. layoutPath
    LayoutPath在CWebApplication中定义,请参阅details in the wiki 所以你可以使用: Yii::app()->layoutPath= Yii::getPathOfAlias('administration.views.layouts');

  2. PeterM回答

  3. 重写 请参阅CController layout attribute

    的说明
      

    要应用于此控制器视图的布局名称。默认为null,表示使用应用程序布局。如果为false,则不应用任何布局。如果控制器属于模块并且此布局属性为null,则将使用模块布局。

  4. 因此您需要将其设置为null并使用模块布局。 注意:在控制器的任何操作中,您可以将布局设置为null $this->layout=null;,这样布局将仅在此操作中为空,而不在其他操作中为空!

答案 1 :(得分:0)

您可以将以下代码放入控制器中。

public function init()
{
    Yii::$app->setLayoutPath($this->module->getBasePath().'/views/layout');
}

或者您可以将以下代码放在模块引导类

/**
 * @inheritdoc
 */
public function init()
{
    parent::init();

    Yii::$app->setLayoutPath($this->getBasePath().'/views/layout');
}