Yii2:从主应用程序渲染模块视图

时间:2016-09-03 06:21:29

标签: model-view-controller yii2 yii2-module

假设模块遵循Controller中列出的目录结构,Yii2应用程序documentation(不是模块控制器)如何呈现模块提供的视图?

1 个答案:

答案 0 :(得分:2)

如方法render()中所述,您可以将视图指定为:

  • 路径别名(例如" @ app / views / site / index");应用程序中的绝对路径(例如" // site / index"):视图名称以双斜线开头。将在应用程序的视图路径下查找实际的视图文件。
  • 模块内的绝对路径(例如" / site / index"):视图名称以单斜杠开头。将在$ module。
  • 的视图路径下查找实际的视图文件
  • 相对路径(例如" index"):将在$ viewPath下查找实际的视图文件。

因此,在您提到的模块的情况下,您可以在操作中执行此操作:

return $this->render('@app/modules/forum/views/default/index');

这将使用主应用程序的应用布局呈现视图。要使用模块的布局,请在操作中添加以下内容:

$this->layout = '@app/modules/forum/views/layouts/main';

这假设default/index模块中的视图main和布局forum