A
A
Anton R.2019-12-01 23:32:48
Yii
Anton R., 2019-12-01 23:32:48

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:
5de422a86e561010871003.jpeg
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

2 answer(s)
M
Moses Fender, 2019-12-02
@mosesfender

A module is "complete software blocks consisting of models, views, controllers, and other supporting components."

It is possible to think so. Only, let's say, in your application, or maybe not even in one, but at least in two (website and admin) there is a model in / common / models, and you need to use it in a certain module of the same application, you will clone it into a module, or will you use the one in /common/models?
For purity, you can, of course, make the successor of that model in the module itself. But what about, for example, helpers. For some reason it seems to me that every developer has at least inherited Html, ArrayHelper, StringHelper, etc. Doge them in modules for completeness to produce?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question