Yii Framework Forum: Yii2 load modules - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii2 load modules Load external-site modules Rate Topic: -----

#1 User is offline   marco_ber 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 08-June 16

  Posted 18 July 2017 - 09:58 AM

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?
0

#2 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 516
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 20 July 2017 - 08:19 PM

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
---config
---main.php
---params.php
--controllers
--messages
--models
--views
-Module.php

and a module's config file is something like

<?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
];



All this does is
1. loop through the modules directory or in your case common modules directory
2. find all config files
3. Merges config files with application config
4. Runs application like normal
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users