Change Extension Configuration On-The-Fly

I have an init() function overwritten in my Controller class. I wanted to change application configuration there. It has to be done dynamically (there) and can’t be permanent, because part of configuration should change only upon some condition is met.

I was able to test, that this thing works, by dynamically changing application langauge:


public function init()

{

    Yii::app()->configure

    (

        array

        (

            'language'=>'en_us'

        )

    );

)

but I failed completely, when I tried to change some extension configuration:


public function init()

{

    Yii::app()->configure

    (

        array

        (

            'components'=>array

            (

                'bootstrap'=>array

                (

                    'class'=>'ext.bootstrap.components.Bootstrap',

                    'responsiveCss'=>false

                )

            )

        )

    );

}

Here I’m trying to force Bootstrap extension to use responsive design, in case user is using a mobile browser. I don’t know why, but it fails completely. Not only I can’t change Bootstrap configuration this way, but even nulling (emptying) whole extension array does not work – i.e. I can still use it in my app.

What am I doing wrong? Why I can change some base app settings (language), but can’t do the same with extension configuration.

Alternatively, maybe someone knows a betterway (directly in application config) to set one or more extension parameters, when a browser is detected to be a mobile one (I have a great function that works basing only on $_SERVER[‘HTTP_USER_AGENT’] and regular expression, does not uses anything out of Yii, so can be called outsite Yii or when Yii aplication isn’t even initiated).

I think changing configuration for instantiated components will not work. You should set their properties explicite:




Yii::app()->bootstrap->responsiveCss = false;



and so on… configuration will only work for components that were not instantiaded. Alternatively you could pass new instances (looking at CModule::setComponents this should work):




            'components'=>array(

                'bootstrap'=>Yii::createComponent( array(

                    'class'=>'ext.bootstrap.components.Bootstrap',

                    'responsiveCss'=>false

                ) )

            )



then new component instance will replace old one

Thanks,

The direct access:


Yii::app()->bootstrap->responsiveCss = false;

works fine! Somehow missed that one.

Actually, for this particular example, this fails, problably because Bootstrap is loading CSS on initation, so changing to responsive CSS later fails.

But I manage to fix that different way.

Yes… that is what I do not like in bootstrap extension: it is recommended to add it to ‘preload’ and during initialization it adds some JS and CSS scripts…

you could however remove it from ‘preload’ section and force initialization in your Controller::init() like this:




if(need to change some bootstrap properties) {

  Yii::app()->setComponents( array( 'bootstrap'=>array(...) ), true );

}

Yii::app()->bootstrap; //this will initialize boostrap



That is so simple, yet perfect solution. Thank you!

I wanted to be far from messing directly in configuration file and with your help I can avoid that! :]