我正在尝试获取模块中的工作布局。所以我在模块的视图文件夹中创建了一个名为"adminLayout"的布局
假设init()方法中AdminModule.php
中的布局。现在看起来是这样的:
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"添加到控制器中,它起作用了。
不知道是什么问题。
此外,我还尝试将布局设置添加到config文件夹中的main.php
,但仍然没有任何操作。如果有人能帮忙,我会很感激的。
解决方案是在模块中的beforeControllerAction上设置布局。它应该起作用。
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
$controller->layout = 'adminLayout';
return true;
}
else
return false;
}
有很多关于这个主题的帖子,答案在Yii文档中:
布局属性
公共混合$布局;
该模块内的控制器共享的布局如果控制器已显式声明其自己的布局,则此属性将被忽略如果为null(默认值),则将使用应用程序的布局或父模块的布局(如果可用)。如果这是false,那么将不使用布局。
只需从控制器中检测模块,并相应地设置布局:
class Controller extends CController
{
public function init(){
//Set layout
$this->layout = ($this->module->id=='admin') ? '//layouts/column2' : '//layouts/column1';
.........
}
在模块中创建资产文件夹。为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;
}
导入所有CSS
和JS
文件,如:
<link rel="stylesheet" type="text/css" href="<?php echo $this->module->assetsUrl; ?>/css/style.default.css" media="screen, projection" />