elangurlmanager Create seo-friendly language urls

  1. Features
  2. Requirements
  3. Installation
  4. Usage
  5. Changelog
  6. Similar extensions

This component extends CUrlManager and adds language features.

Features

There are similar extensions, but this one supports language cookie and UI components for switching the language.

Requirements

Developed with Yii 1.1.12

Installation

Extract the ELangUrlManager.php into protected/components

Configure the urlManager in config/main.php

components => array(
...

'urlManager'=>array(
             //ELangUrlManager configuration
            'class'=>'ELangUrlManager',
            'languages'=>array('de'=>'Deutsch','en'=>'English'), //assoziative array language => label
            'cookieDays'=>10, //keep language 10 days
            //'languageParam'=>'lang' //=default

            //common configuration for the yii urlManager
            //don't add 'language' rules here, these rules will be added by the ELangUrlManager 
            'urlFormat'=>'path',
            'showScriptName'=>false,
			'rules'=>array(
				'<controller:\w+>/<id:\d+>'=>'<controller>/view',
				...
			),
            .....
        ),
...
)

Thats all.

  • The ELangUrlManager adds the language rules as first items to the rules. See code comment ELangUrlManager::addLanguageUrlRules. No need for adding the language rules manually.

  • Adds the current language to all urls created with Yii::app()->creatUrl or controller->createUrl

Usage

Switch between languages:

// a language dropdownlist
echo Yii::app()->urlManager->getLanguageDropDown();

//a list of links
$links = Yii::app()->urlManager->getLanguageImageLinks();
foreach($links as $link)
         echo $link .'<br/>';

//a image link list with the array(language=>image,...) as first param 
$links = Yii::app()->urlManager->getLanguageImageLinks(array('de'=>'images/flags/de.gif', ....));
 foreach($links as $link)
         echo $link .'<br/>';

More params/methods: Take a look at the code.

Add a language switch to a bootstrap navbar (requires Yiibooster).

$this->widget('bootstrap.widgets.TbNavbar', array(
        'items' => array(
            array(
                'class' => 'bootstrap.widgets.TbMenu',
                'items' => array(

                    //your menu items   
                    array('label' => 'Home', 'url' => array('site/index')),
                    ...
                    
                )
            ),
            //the language switch
            array(
                'class'=>'bootstrap.widgets.TbMenu',
                'htmlOptions'=>array('class'=>'pull-right'),
                'items'=>Yii::app()->urlManager->getLanguageMenuItems(),
            ),
        )
    ));

Note: The component works together with the menubuilder, because the menubuilder does a Yii::app()->createUrl on rendering the menu items.

Changelog

  • v1.1 added method getLanguageMenuItems

Similar extensions

0 0
4 followers
611 downloads
Yii Version: Unknown
License: BSD-2-Clause
Category: Others
Developed by: Joblo
Created on: Jun 27, 2013
Last updated: 11 years ago

Downloads

show all

Related Extensions