model-view-controlleryii2yii2-module

Yii2: Render module view from main application


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?


Solution

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

    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';
    

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