Yii2:从主应用程序渲染模块视图
Yii2: Render module view from main application
一个Yii2应用如何Controller (not a module controller) render a view that is provided by a module, assuming the module follows the directory structure outlined in the documentation?
如方法 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';
这假定 forum
模块中的视图 default/index
和布局 main
。
一个Yii2应用如何Controller (not a module controller) render a view that is provided by a module, assuming the module follows the directory structure outlined in the documentation?
如方法 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';
这假定 forum
模块中的视图 default/index
和布局 main
。