Answer the question
In order to leave comments, you need to log in
Yii2 - How to display a module in the View of the main page?
I am reading the official Yii2 doc regarding Modules: https://www.yiiframework.com/doc/guide/2.0/ru/stru... In principle, everything is clear and logical, but given the previous experience with CMS, an acute problem immediately arose the desire to mold a test module and display it, for example, in the sitebar of the main page:
And here a plug arose, how to display it in a "foreign" action of a foreign controller , if the module is "complete software blocks consisting of models, views, controllers and other auxiliary components. "?
A link like www.site.ru/samplemodule opens the module, everything is OK, what are the ways to integrate it into, say, www.site.ru/index? if the module has its own view, controller and even model. Or will you have to do all such things as widgets?
Now the idea is this - in the method of the controller of the Main page, call the Module object, carry out all the stages of the work of its methods up to render(), but at the end pack everything into the $module variable and send it to the Main page view with other data.
If it's rough:
namespace app\controllers;
use app\modules\banner\Banner;
class SiteController extends Controller
{
public function actionIndex(){
$banner = new Banner();
$data = 'Lorem ipsum...';
return $this->render('index', [
'banner' => $banner,
'data' => $data,
])
}
}
namespace app\modules\banner;
class Banner extends \yii\base\Module
{
public $controllerNamespace = 'app\modules\banner\controllers';
public function init()
{
parent::init();
}
}
namespace app\modules\banner\controllers;
use yii\web\Controller;
class DefaultController extends Controller
{
public function actionIndex()
{
return $this->render('index');
}
}
Answer the question
In order to leave comments, you need to log in
A module is "complete software blocks consisting of models, views, controllers, and other supporting components."
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question