How to extend modules

Hi all,

I have the following issue. I have Module1 with controllerA and ControllerB. I want to extend the module with MyModule with Controller C. But here is the problem - I expect that the final Module should have contollers A,B and C but this doesn’t happen. If I use $controllerMap there is still problem with different view paths for each controller.

How can we extend modules by keeping their original controllers, adding new ones or overriding them ?

is it possible to extend modules in Yii2 ?

I have created issue on github, but I’m moving the discussion here. @samdark said that this is bad practise and it’s not good idea to use modules like that, but sam how are we gonna achieve this ? what’s the yii’s way ?

thx

What’s the module you’re trying to reuse? Why?

Well actually they are both my modules :) . I’ll try to explain :

I have a yii-user module which provides basic functionalities like register, login, basic user management etc.

Then I want to create a second module that extends the functionalities of the yii-user module by adding groups for users, group access control support. Also this new module adds some interfaces for managing groups and access rights based on these groups. So I want to EXTEND the yii-user modules by adding new GUIs for editing but keeping the old ones - which i a problem.

The main idea is to have basic user extension and some “addons” depending on the purpose of your app. Currently i’m developing an intranet site for a big company and would like to use group based access control and each user belongs to a group(job title).

But the question is more general - is it possible to really extend modules, and if not what’s the way to achieve this “addon” like concept. The problem I encounter is mainly in the controller/view area - the other things seem clear.

Well, it all depends on the module you’re trying to customize. yii-user isn’t ideal example in this regard.

If you want to replace/add some buttons to the views defined in yii-user I don’t think it’s really possible but if you want separate pages with alike URLs you can implement these independently and then adjust URLs via URL manager to look like these belong to yii-user.

Personally I’m not using any user management module for exactly the same reason. None are customizable.

Well, the module is nkostadinov\yii2-user but didn’t want to publish it yet because its still in development. May be the module extension approach is not good in the current yii architecture.

I’ll just create a component and will “inject” the controller’s via controllerMap. But this problem got me thinking - why controllers are so dependent on the module … just for the viewPath, wouldn’t it be better if they are more decoupled ? This way each controller may have it’s own views(no matter where) and in my case I would just override this property.

Controller can have its views independent but you have to use path alias to refer to then when calling render.

Yes, I know that, and may be I’ll use this as workaround, but it’s not an elegant solution :)

I finally did it this way :

[list=1]

[*]Every controller extends this BaseController


class BaseController extends Controller {


    public $viewPathOverride;


    public function getViewPath()

    {

        if(!isset($this->viewPathOverride))

            return parent::getViewPath();

        else

            return $this->viewPathOverride . DIRECTORY_SEPARATOR . $this->id;

    }


}

[*]The module then look like this:


class Module extends BaseModule

{

    public $controllerMap = [

        'security' => [

            'class' => 'nkostadinov\user\controllers\SecurityController',

            'viewPathOverride' => '@vendor/nkostadinov/yii2-user/views'

        ],

        'registration' => [

            'class' => 'nkostadinov\user\controllers\RegistrationController',

            'viewPathOverride' => '@vendor/nkostadinov/yii2-user/views'

        ],

        'admin' => [

            'class' => 'nkostadinov\user\controllers\AdminController',

            'viewPathOverride' => '@vendor/nkostadinov/yii2-user/views'

        ],

    ];


}

[/list]