Force extension to be available only in a module

In this post we have an example of some Yii extension available only in module and not available in main application. I’ve put that example there, because I haven’t got any other solution ready, when writing that article.

But the fact is, that the very same solution can be achieved in much easier way.

If you want any module to be available only in a particular module, then remove it from main application’s configuration file, from ‘preload’ section. And then, in your module’s init() function add following line:

Yii::app()->getComponent('bootstrap');

So, your module-specific extension configuration still remains in your main application’s configuration, but isn’t preloaded by main application. It is loaded (used and made available) in module only with above line.

This solution is usable, when your component is registering any CSS files that may influence your app look or any JS files that may run into collision with other Javascript you use (i.e. duplicated function names etc.).

It is also a semi-professional approach of keeping entire configuration (for both main application and all modules) in one place (instead of spreading it among module files) and only requiring (loading) extensions when they’re really needed (to avoid above mentioned collisions).

Leave a Reply