robertobadjio
robertobadjio

Reputation: 185

how to override Controller, Model, Views in yii2

How to override the class module in the config of my theme? I tried so impossible.

return [
   ...
   'modules' => [
       'shop' => [
        'class' => 'app\modules\shop\ShopModule',
           'components' => [
               'manager' => [
                   'class' => 'app\web\theme\modules\shop\Customer',
               ],
           ],
    ],
...
  ],

];

Upvotes: 2

Views: 3334

Answers (1)

Amitesh Kumar
Amitesh Kumar

Reputation: 3079

Overriding controllers

Sometimes you may need to override default Yii2-user controllers. It is pretty easy and takes two steps. Step 1: Create new controller

First of all you need to create new controller under your own namespace (we’d recommend app\controllers\user) and extend it from needed Yii2-user controller.

For example, if you want to override AdminController you should create app\controllers\user\AdminController and extend it from dektrium\user\controllers\AdminController:

<?php

namespace app\controllers\user;

use dektrium\user\controllers\AdminController as BaseAdminController;

class AdminController extends BaseAdminController
{
    public function actionCreate()
    {
        // do your magic
    }
}

Step 2: Add your controller to controller map

To let Yii2-user know about your controller you should add it to controller map as follows:

<?php return [
    ...
    'modules' => [
        ...
        'user' => [
            'class' => 'dektrium\user\Module',
            'controllerMap' => [
                'admin' => 'app\controllers\user\AdminController'
            ],
            ...
        ],
        ...
    ],

For overriding view click here

Upvotes: 2

Related Questions