Yii2 load modules

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 wich 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-modules/

    module-1/

    module-2/



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

You could dynamically grab your modules

you site one and site 2 modules would be something like





$params = require(__DIR__ . '/params.php');

//path to common modules

Yii::setAlias('@modules', dirname(dirname(__FILE__)) . '/modules/');

$config = [

    'modules'=>[

        //your site 1 or site 2 modules

    ],

    'params' => $params,

];

//make sure your file path is right

//get common modules

$moduleDirectory = new DirectoryIterator(Yii::getAlias('@modules'));

foreach ($moduleDirectory as $module) {

    $moduleConfig = Yii::getAlias('@modules') . DIRECTORY_SEPARATOR . $module->getFilename() . '/config/main.php';

    if ($module->isDir() && !$module->isDot() && file_exists($moduleConfig)) {

        $config = yii\helpers\ArrayHelper::merge(require $moduleConfig, $config);

    }

}

return $config;



my module structre is something like

modules

-module Name

–[size="2"]-config[/size]

[size="2"]—main.php[/size]

[size="2"]—params.php[/size]

[size="2"]–controllers[/size]

[size="2"]–messages[/size]

[size="2"]–models[/size]

[size="2"]–views[/size]

[size="2"]-Module.php[/size]

[size=“2”]and a module’s config file is something like[/size]

[size="2"]




<?php


return [

    'components' => [

        'i18n' => [

            'translations' => [

                'news' => [

                    'class' => 'yii\i18n\PhpMessageSource',

                    'basePath' => '@app/modules/news/messages'

                ]

            ]

        ],

        'urlManager' => [

            'rules' => [

                'news' => 'news/news/index',

                'news/<id:\d+>' => 'news/news/view',

                'news/<action>' => 'news/news/<action>',

                'news/<alias:delete|restore>/<id:\d+>' => 'news/news/<alias>',

            ]

        ]

    ],

    'modules' => [

        'news' => [

            'class' => 'app\modules\news\Module',

            'controllerNamespace' => 'app\modules\news\controllers',

            'defaultRoute' => 'news/index',

            'params' => require(__DIR__ . '/params.php')

        ]

    ]

//params here

];



[/size]

[size="2"]All this does is [/size]

[size="2"]1. loop through the modules directory or in your case common modules directory[/size]

[size="2"]2. find all config files[/size]

[size="2"]3. Merges config files with application config[/size]

[size="2"]4. Runs application like normal[/size]