如何在 Yii2 中更改控制器的默认视图? [英] How to change default view for controller in Yii2?
本文介绍了如何在 Yii2 中更改控制器的默认视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道我可以在 Yii2 中更改控制器的默认视图文件夹吗?
如果我们可以通过使用public $layout
来改变布局,我们如何用视图来做到这一点?
Class HomeController 扩展 \yii\web\Controller{公共 $layout = 'mylayout';公共 $view = 'newview';公共函数 actionIndex(){返回 $this->render('index');}}
解决方案
为了实现您的控制器应该实现 ViewContextInterface.
use yii\base\ViewContextInterface;使用 yii\web\Controller;类 HomeController 扩展 Controller 实现 ViewContextInterface
然后只需添加 getViewPath() 方法这应该返回所需的目录路径:
公共函数 getViewPath(){return Yii::getAlias('@frontend/views/newview');}
您可以在此处使用别名.
另请查看关于组织视图的官方文档.>
I was wondering can I change default view folder for a controller in Yii2?
If we can change layout just by using public $layout
, how we can do it with view?
Class HomeController extends \yii\web\Controller
{
public $layout = 'mylayout';
public $view = 'newview';
public function actionIndex()
{
return $this->render('index');
}
}
解决方案
To achieve that your controller should implement ViewContextInterface.
use yii\base\ViewContextInterface;
use yii\web\Controller;
class HomeController extends Controller implements ViewContextInterface
Then just add getViewPath() method which should return the desired directory path:
public function getViewPath()
{
return Yii::getAlias('@frontend/views/newview');
}
You can use aliases here.
Also check the official documentation about organizing views.
这篇关于如何在 Yii2 中更改控制器的默认视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文