0
votes

I want to extend a controller from the backend / controller into a my existed module. The structure of directory in My Yii2 Application as follows.

``

  • backend

    • controllers
      • JobOrderController
    • view
      • job-order
  • modules

    • marketing
      • controllers
        • JobOrderController [extend from @backend \ controllers \ JobOrderController]

``

When I access the route: localhost / marketing / job-order, I get an error message:

`` View not Found - yii \ base \ ViewNotFoundException The view file does not exist: ../../advanced/backend/modules/marketing/views/job-order/index.php

``

I don't want to change any view from the marketing module, is it possible?

1
What about setting $viewPath in the module's JobOrderController to @backend/view/job-orders? - Michal Hynčica
But if your goal is to have external controller to appear as part of marketing module, setting it in module's $controllerMap property might be better idea, because that way you don't need to add any extra files into the module's structure. - Michal Hynčica

1 Answers

0
votes

Just use controllerMap in Module config. Also set the view folder.

public function init()
    {
        parent::init();
        
        // custom initialization code goes here
        $this->controllerMap = [
            'job-order' => [
                'class' => 'backend\components\controllers\JobOrderController',
                'viewPath' => Yii::getAlias('@backend') . '/components/views/job-order'
            ]
        ];
    }