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

  1. Requirements
  2. Usage
  3. Resources

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.

Requirements

Tested with yii 1.1

Usage

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()->user->applyPreferedLanguage();
		
		Yii::app()->name = Yii::t('site', Yii::app()->name);
		parent::init();
	}

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));
			$this->setLanguage($lang);
		}
		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'));
	}
}

Resources

...external resources for this extension...

6 0
5 followers
959 downloads
Yii Version: 1.1
License: LGPL-3.0
Category: User Interface
Developed by: Parcouss
Created on: Oct 24, 2010
Last updated: 13 years ago

Downloads

show all

Related Extensions