我在我的网络项目中使用 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/