Yii2:从主应用程序渲染模块视图 [英] Yii2: Render module view from main application

查看:81
本文介绍了Yii2:从主应用程序渲染模块视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Yii2 应用程序如何Controller(不是模块控制器)呈现由模块提供的视图,假设模块遵循 文档?

How can a Yii2 application 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() 您可以将视图指定为:

As mentioned in method render() you can specify view as:

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

因此,对于您提到的模块,请在操作中执行此操作:

So in the case of the module mentioned by you do this in the action:

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

这将使用主应用程序的应用布局呈现视图.要使用模块的布局,也要在操作中添加:

This will render the view with applied layout of the main application. To use module's layout add this as well in the action:

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

这假设在 forum 模块中查看 default/index 和布局 main.

This assumes view default/index and layout main in the forum module.

这篇关于Yii2:从主应用程序渲染模块视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆