yii2-cookbook
yii2-cookbook copied to clipboard
How to configure a module with a config file
class Module extends \yii\base\Module
{
public function init()
{
parent::init();
\Yii::configure($this, require __DIR__ . '/config.php');
}
config.php:
return [
'params' => [ ... ],
'components' => [
// ...
],
];
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
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',
],
],
];
I don't think sub-modules are a good idea.
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?
No idea. Try it.
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)
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',
],
]