Alternative module configuration that does not affect main configuration

In Yii main application is actually a module (core one) so each module configuration actually shares nearly everything what you can put to main application’s configuration file.

Thus, you can configure any Yii module, just as you would do with your main application. The only difference is that you don’t use external configuration file, but CModule::configure() function instead.

Here is an example of such in-module configuration

[code language=”php”]
public function init()
{
$this->configure
(
array
(
‘preload’=>array(‘bootstrap’),
‘components’=>array
(
‘bootstrap’=>array
(
‘class’=>’admin.extensions.bootstrap.components.Bootstrap’,
‘coreCss’=>true,
‘responsiveCss’=>true,
‘yiiCss’=>true,
‘enableJS’=>true
)
)
)
);

$this->preloadComponents();
}
[/code]

Above example also shows how to preload Yii Bootstrap extension inside module and thus make it available in that module only. This causes and Bootstrap’s styles does not affects main application look.

Keep in mind, that with configure() function you can only change settings of uninitiated components. For those, that are already initiated, you should use a direct approach. I.e.:

[code language=”php”]
Yii::app()->bootstrap->responsiveCss = false;
[/code]

Refer to this Yii forum topic for more details.

The configure() function is also a member of CWebApplication function (because — as said — core application is actually a module), which means that you can use:

[code language=”php”]
Yii::app()->configure($configArray);
[/code]

to alter main application configuration on-the-fly.


UPDATE: I used solution shown in this article, because I haven’t got any better ready, when writing this post. Actually, this particular thing (loading an extension only for module, not for main application) can be achieved much easier way. Read this post for more details.

Leave a Reply