Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to configure a module with a config file #169

Open
samdark opened this issue Mar 29, 2017 · 7 comments
Open

How to configure a module with a config file #169

samdark opened this issue Mar 29, 2017 · 7 comments

Comments

@samdark
Copy link
Owner

samdark commented Mar 29, 2017

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

config.php:

return [
    'params' => [ ... ],
    'components' => [
         // ...
    ],
];
@memobr
Copy link

memobr commented Apr 7, 2019

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
Copy link

host138 commented Aug 17, 2019

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
Copy link
Owner Author

samdark commented Aug 17, 2019

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

@host138
Copy link

host138 commented Aug 17, 2019

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
Copy link
Owner Author

samdark commented Aug 17, 2019

No idea. Try it.

@speker2010
Copy link

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
Copy link

uldisn commented Feb 26, 2020

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',
        ],
 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants