S
S
Sergey Beloventsev2017-06-17 07:21:37
Yii
Sergey Beloventsev, 2017-06-17 07:21:37

How to get the module correctly?

I'm trying to solve the issue with my extension , here's the link, the essence of what I need to display the generated menu as is, I came to the conclusion that this will have to be done through Menuviews::widgets([]);
accordingly, in the directory of its extension, I created the Menuviews class, which inherited from Widget
, here is the extension scheme

assets 	//директрория
  controllers 	//директория
  messages/ru-RU/sirgalas/menu 	//директория
  migrations 	//директория
  models 	//директория
  views 	//директория
  MenuAsset.php 	//асеты
  MenuModule.php 	//сам модуль
  MenuView.php      //то с чем пытаюсь работать

why is this being done what would be like this
use sirgalas\menu\MenuView;
<?= MenuView::widget(['name'=>'HederMenuLeft']); ?>

getting the generated menu
has now become a problem getting the extension module itself into the MenuView module is called MenuModule.
do it like this
use sirgalas\menu\models\Menu;
use yii\base\Widget;
use sirgalas\menu\MenuModule;
class MenuView extends Widget
{
    public $name;
    public function init(){
        parent::init();
    }
    public function run(){
        $model=Menu::findOne($this->name);
        $module= MenuModule::getInstance();
        return var_dump($module);
    }

I get null
There were two questions 1 there is a feeling that I'm doing shit code is it so? 2 why can't I get the MenuModule in the MenuView class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-06-19
@qonand

There were two questions 1 there is a feeling that I'm doing shit code is it so?

For starters, what does your extension do to form menus? In this case, I don’t understand why it has a model and migrations. The model should be injected into your module, and not live in it .... Next MenuView - what kind of widget is it, what is it responsible for? for drawing the menu? If so, then I don’t see the point in it at all, because there are standard tools
Because getInstance() returns an instantiated class object. And if the current request does not belong to the specified module, the module is not instantiated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question