Yii2 load modules

863 Views Asked by At

How I can load an external-site module? I have a common module I need to load in distinct Yii2 sites, like advanced-template my idea is to have a common dir where store generic modules which I can load to each site. A file system structure can be like this:

/
  site-1/
    (loads modules from common-modules dir for site-1)
  site-2/
    (loads modules from common-modules dir for site-2)
  common_sites_modules/
    module-1/
    module-2/
    carrello/
        Carrello.php

Each site in his configuration have to load modules from common-modules/ Is possible to implement this structure?

Edit 1

The configuration:

'cart' => [
    'class' => dirname(dirname(dirname(__DIR__))) . '/common_sites_modules/carrello/Carrello',
    'params' =>[
        ...
    ],
    'components' => [
        ...
    ],
],

and this is the first line of the class Carrello.php:

<?php
namespace common_sites_modules\carrello;
...

The top bar of editor with the path of the class and the error returned by Yii: Sublime top bar

enter image description here

Edit 2:

Thanks to @Yupik for support and suggests, the new settings:

bootstrap.php:

Yii::setAlias('@common-modules', dirname(dirname(dirname(__DIR__))) . '/common_sites_modules');

main-local.php:

'class' => '@common-modules\carrello\Carrello',

The generated error:

enter image description here

Like suggested in the comments the solution is to declare an alias and then use the name of alias for call the module. Like suggested by @Yupik I've set in the common/config/bootstrap.php an alias as follow:

Yii::setAlias('@common_modules', dirname(dirname(dirname(__DIR__))) . '/common_modules');

In the main configuration:

'carrello' => [
    'class' => ''common_modules\carrello\Carrello',
     ...
]

Obviously namespace have to be configured based on the position on filesystem. Thanks for the suggestions

1

There are 1 best solutions below

0
Navneet Singh Chouhan On

Yes, we can do this by making a symlink of common_sites_modules directory in both site folder (site1, site2).