Yii 1.1: How to use an application behavior to maintain runtime configuration

26 followers

In this tutorial will be explained a method to manage some configuration runtime. This excellent tutorial follows a similar approach, but requires to write a masterclass which all controllers are supposed to extend, following this wiki you can achieve the same by only editing the configuration.

This is often needed to manage internationalization, themes and other configuration that depends from the user.

The most comfortable way is to user an application behavior. In this example we save the language in the session, but it can also be saved in the database.

Create a file in components named ApplicationConfigBehavior and copy this code:

<?php
 
/**
 * ApplicationConfigBehavior is a behavior for the application.
 * It loads additional config parameters that cannot be statically 
 * written in config/main
 */
class ApplicationConfigBehavior extends CBehavior
{
    /**
     * Declares events and the event handler methods
     * See yii documentation on behavior
     */
    public function events()
    {
        return array_merge(parent::events(), array(
            'onBeginRequest'=>'beginRequest',
        ));
    }
 
    /**
     * Load configuration that cannot be put in config/main
     */
    public function beginRequest()
    {
       if (isset($_POST['lang']))
               $this->owner->user->setState('applicationLanguage', $_POST['lang']);
       if ($this->owner->user->getState('applicationLanguage'))
            $this->owner->language=$this->owner->user->getState('applicationLanguage');
        else 
            $this->owner->language='en';
    }
}

This file must be included in config/main.php:

'behaviors' => array('ApplicationConfigBehavior')

To update the language we can use a widget (to be included in layout/main), for example:

components/LangBox.php

<?php
class LangBox extends CWidget
{
    public function run()
    {
        $currentLang = Yii::app()->language;
        $this->render('langBox', array('currentLang' => $currentLang));
    }
}
?>

components/views/langBox.php

<?php echo CHtml::form(); ?>
    <div id="langdrop">
        <?php echo CHtml::dropDownList('lang', $currentLang, array(
            'en_us' => 'English', 'is_is' => 'Icelandic'), array('submit' => '')); ?>
    </div>
<?php echo CHtml::endForm(); ?>

Total 3 comments

#5842 report it
qinsanep at 2011/11/19 01:52pm
drop down id does not match $_POST id in ApplicationConfigBehavior

Hey guys, just a quick heads up. If this widget is not functioning properly, it could be because the id of the drop down is '_lang' and we refer to the post data as 'lang'. Just make them they have same name and it should now operate fine.

Would there be any reason we would want to name the id _lang, with the underscore?

#5592 report it
Aladdin at 2011/10/23 05:56am
this is the best approach

I have looked at 3 other approaches http://www.yiiframework.com/wiki/210/multilanguage-web-site-controlling-by-get-request-and-database-allowed-languages/ http://www.yiiframework.com/extension/langhandler/ http://www.yiiframework.com/wiki/26/setting-and-maintaining-the-language-in-application-i18n/

anyhow, this tutorial is the best, the neater and the straighter solution. thank you zaccaria

#5061 report it
jumamidin at 2011/09/12 02:13am
I couldn`t make widget work

hi zaccaria, I followed this wiki. But do not make it work.

I see I cannot make dropDownList( , , , array('submit'=>'')); This button didn`t make me take to function in widget or ApplicationConfigBehavior. where I am really confuse in.

In action\controller when we use form, the form`s submit button knows which action to render. But in widget I can not show which function has to render for clicking submit button.

Please, help me.

Leave a comment

Please to leave your comment.

Write new article