我想知道我可以更改 Yii2 中 Controller 的默认 View 文件夹吗?
如果我们可以通过使用 public $layout
来改变布局,我们如何用 View 来做呢?
Class HomeController extends \yii\web\Controller
{
public $layout = 'mylayout';
public $view = 'newview';
public function actionIndex()
{
return $this->render('index');
}
}
最佳答案
要实现这一点,您的 Controller 应该实现 ViewContextInterface .
use yii\base\ViewContextInterface;
use yii\web\Controller;
class HomeController extends Controller implements ViewContextInterface
然后只需添加 getViewPath()应返回所需目录路径的方法:
public function getViewPath()
{
return Yii::getAlias('@frontend/views/newview');
}
你可以在这里使用别名。
又查官方documentation关于组织 View 。
关于php - 如何更改 Yii2 中 Controller 的默认 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27698934/