Yii 1.1: dropdownredirect

A dropdownlist widget which can redirect user based on a selected value

It is a simple widget embedding a dropdownlist to redirect on a selected value. I used this to allow the user to change the language.


Tested with yii 1.1


Put the DropDownRedirect.php file in your components directory.

I'll show you the way I used this. In my main layout file, I wrote :

$this->widget('DropDownRedirect', array(
    'data' => Yii::app()->user->avalaibleLanguages, // data od my dropdownlist
    'url' => $this->createUrl($this->route, array_merge($_GET, array('lang' => '__value__'))), // the url (__value__ will be replaced by the selected value)
    'select' => Yii::app()->user->language, //the preselected value

The dropdown is now placed. To handle the url redirection, I wrote in my controller :

public function init() {
        if (isset($_GET['lang']))
            Yii::app()->user->language = $_GET['lang'];
        Yii::app()->name = Yii::t('site', Yii::app()->name);

The user functions used above are here, in a personnal WebUser :

class WebUser extends CWebUser {
    public function getLanguage() {
        if ($this->getState('lang') == null) {
            $lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
        return $this->getState('lang');
    public function setLanguage($lang) {
        $this->setState('lang', $lang);
    public function applyPreferedLanguage() {
        Yii::app()->language = $this->getLanguage();
    public function getAvalaibleLanguages() {
        return array('en' => Yii::t('site', 'english'), 'fr' => Yii::t('site', 'french'));


...external resources for this extension...

Total 5 comments

#2128 report it
jacmoe at 2010/11/15 01:14pm
This works

This version works in Yii 1.1.5:

 * @brief This widget is a dropDown which can redirect to  an url base on selected value via javascript
class DropDownRedirect extends CWidget {
    public $name; // name attribute of the dropdownlist
    public $select; // selected value
    public $data; // data of the dropdownlist
    public $htmlOptions = array(); // options of the dropdownlist
    public $url; // url with the string $replacement somewhere, wich will be replaced by the current value
    public $replacement = '__value__'; // will be replaced by the value
    public function init() {
        if (! isset($this->name))
            $this->name= $this->id;
    public function run() {
        if (!isset($this->htmlOptions['id'])) $this->htmlOptions['id'] = $this->id;
                $script = 'window.location = "'.$this->url.'".replace("'.$this->replacement.'", $(this).val());';
                if (!isset($this->htmlOptions['onChange'])) $this->htmlOptions['onChange'] = $script;
                echo CHtml::dropDownList($this->name, $this->select, $this->data, $this->htmlOptions);
#2123 report it
jacmoe at 2010/11/14 08:49pm
Doesn't work in Yii 1.1.5

For some reason, this extension stopped working by Yii 1.1.5. Would be great if someone could fix it. :)

#2019 report it
jacmoe at 2010/11/01 07:50pm

Since we can't edit our comments, let me correct the criteria select statement:

$Criteria->select = "name";

'id' is not needed, of course. My Project and my Issue controller makes use of the 'name' parameter to determine the active project.

#2018 report it
jacmoe at 2010/11/01 07:46pm
Great work!

I've successfully integrated this with my application where I use it to allow my users to switch active project.

I my Project and Issue controller I have this function:

public function getProjects() {
            $Criteria = new CDbCriteria();
            $Criteria->select = "id, name";
            $results = Project::model()->findAll($Criteria);
            $project_list = array();
            $project_list[''] = '<Switch Project>';
            foreach($results as $result) {
                $project_list[$result->name] = $result->name;
            return $project_list;

I use this code to place the widget:

if (((Yii::app()->controller->id === 'project')||(Yii::app()->controller->id === 'issue')) && (isset($_GET['name']))) {
            $this->widget('DropDownRedirect', array(
                'data' => Yii::app()->controller->getProjects(),
                'url' => $this->createUrl($this->route, array_merge($_GET, array('name' => '__value__'))),
                'select' => '<Switch Project>', //the preselected value
                'htmlOptions' => array('class' => 'operations')

Thanks a lot!

#1984 report it
Parcouss at 2010/10/25 03:10pm
Oups, I forgot availableLanguages function :)

This is in my WebUser class :

public function getAvalaibleLanguages() {
    return array('en' => Yii::t('site', 'english'), 'fr' => Yii::t('site', 'french'));

Leave a comment

Please to leave your comment.

Create extension