我正在尝试在高级yii2中做多主题。我为此尝试了很多方法,但我无法理解。首先,我将其添加到"前端/配置/主.php";
'view' => [
'theme' => [
'pathMap' => [
'@app/views' => [
'@webroot/themes/demo/views',
]
],
],
],
它不起作用,然后我尝试为前端创建一个新的视图类,例如:
namespace frontend'components;
class NewsView extends 'yii'web'View {
function init() {
'Yii::$app->view->viewPath = '@webroot/themes';
parent::init();
}
}
并添加到配置中.php
'view' => [
'class' => 'frontend'components'NewsView',
但它也不起作用。
我该怎么办?
您可以在基本控制器上重新定义 getViewPath 方法。喜欢
public function getViewPath()
{
return Yii::getAlias('@frontend/views/newview');
}
在控制器中,您也可以在 init 方法中更改视图路径:
public function init() {
parent::init();
$this->viewPath = '@frontend/views/newview'; // or before init()
}
或者使用已经建议的方式。
给像我这样的白痴的提示:不要试图覆盖$viewPath
属性。
class MyController extends Controller {
public $viewPath = '@frontend/views/newview'; // Doesn't work
}
这是行不通的。实际上,私有成员$_viewPath
需要在基本控制器中设置!它将在渲染时进行评估(如果参数 $view
是相对路径)。 $this->viewPath = '...';
调用一个设置$_viewPath
的 setter 函数。如果您用public $viewPath = '...';
覆盖$viewPath
则无法再调用魔术设置器函数,因此$_viewPath
将不会更改。