samdark / yii2-cookbook

Yii 2.0 Community Cookbook
1.45k stars 296 forks source link

How to configure a module with a config file #169

Open samdark opened 7 years ago

samdark commented 7 years ago
class Module extends \yii\base\Module
{
    public function init()
    {
        parent::init();
        \Yii::configure($this, require __DIR__ . '/config.php');
}

config.php:

return [
    'params' => [ ... ],
    'components' => [
         // ...
    ],
];
memobr commented 5 years ago

i cant figure out if its a bug or something else. i configure module exactly as above mentioned by @samdark . somehow config file is ignored for example mailer component viewPath is ignored i cant overide

//module config file
return [
  'params' => [],
  'components' => [
    'mailer' => [
      'class' => 'yii\swiftmailer\Mailer',
      'useFileTransport' => true,
      'viewPath' => '@app/modules/user/mail'
    ],
  ],
];

The view file does not exist: C:\xampp\apps\testing/mail\PasswordRequest.php

host138 commented 4 years ago

Is it right to write in the config's module some child modules or i can to use only "modules" property (https://www.yiiframework.com/doc/guide/2.0/en/structure-modules#nested-modules)?

Something like this (config in the module's directory):

return [
  'params' => [],
  'components' => [],
  'modules' => [
        'Submodule' => [
            'class' => 'app\modules\Test\modules\Submodule\Submodule',
        ],
        'Submodule2' => [
            'class' => 'app\modules\Test\modules\Submodule2\Submodule2',
        ],
    ],
];
samdark commented 4 years ago

I don't think sub-modules are a good idea.

host138 commented 4 years ago

I transfer from yii 1 and there uses nested modules by importing and i need to keep the structure. That config in the example will work properly or have to use only "modules" property?

samdark commented 4 years ago

No idea. Try it.

speker2010 commented 4 years ago

Maybe better is:

use yii\helpers\ArrayHelper;

class Module extends \yii\base\Module
{
    public function  __construct($id, $parent = null, $config = [])
    {
        $config = ArrayHelper::merge(
            require __DIR__ . '/config.php',
            $config
        );
        parent::__construct($id, $parent, $config);
}

In this case you can override defaults in app's config(e.g. common/config/main-local.php for advanced template)

uldisn commented 4 years ago

In projects use follow base module:

<?php

namespace d3system\yii2\base;

use yii\base\Module;

class D3Module extends Module
{
    public $configFilePath;

    /**
     * @var array panels for PanelWidgets
     */
    public $panels;

    public function __construct($id, $parent = null, $config = [])
    {
        if(isset($config['configFilePath'])){
            $config = array_merge($config,include $config['configFilePath']);
        }
        parent::__construct($id, $parent, $config);
    }

}

in config file by setting define, where is module config file:

    'modules' => [
        'wiki'=>[
            'class'=>'asinfotrack\yii2\wiki\Module',
            'configFilePath' => __DIR__ . '/module_wiki.php',
        ],
 ]