layout - 对 Yii 中的多个模块使用通用布局

标签 layout module yii

我在我的网络项目中使用 Yii 框架。现在,我有几个模块,我想为所有模块仅使用一种布局。我使用以下代码来确定每个模块中每个 Controller /操作的布局:

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

是否有其他解决方案可以通过在每个模块的 init() 函数中使用相同的代码来实现此目的? 换句话说,我必须在每个 Action 中编写上面的两行代码,我认为这不好,我想减少我的代码行数。例如如下:

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

但它不起作用。请帮助我。

最佳答案

只需使用

$this->layout='//layouts/myLayout';

没有

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

因为//意味着你从根开始的特定绝对路径

关于layout - 对 Yii 中的多个模块使用通用布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12881770/

相关文章:

layout - Aurelia:模板化。在另一个模板中包含一个模板

php - Yii 1.1.7 - 找不到 gii 页面

android - 'textEmailAddress' 和 'textWebEmailAddress' 有什么区别

java - 如何制作与图像形状完全相同的自定义形状 View

Android 3.2 所有屏幕布局相同

php - Yii - CActiveDataProvider 使用关系

php - Yii 多对多过滤

python - Python中跨模块和静态方法访问全局变量

python - 如何导入名称不明确的特定模块?

c - Windows 上带有 DLL 的动态模块