Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@crisu83
Created May 7, 2012 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisu83/2629751 to your computer and use it in GitHub Desktop.
Save crisu83/2629751 to your computer and use it in GitHub Desktop.
<?php
/**
* CmsUrlManager class file.
* @author Christoffer Niska <christoffer.niska@nordsoftware.com>
* @copyright Copyright &copy; 2012, Nord Software Ltd
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @package cms.components
*/
/**
* URL manager that appends the application language to each URL
*/
class CmsUrlManager extends CUrlManager
{
/**
* Constructs a URL.
* @param string $route the controller and the action (e.g. article/read)
* @param array $params list of GET parameters (name=>value).
* @param string $ampersand the token separating name-value pairs in the URL. Defaults to '&'.
* @return string the constructed URL
* @see CUrlManager::createUrl
*/
public function createUrl($route, $params = array(), $ampersand = '&')
{
if (!isset($params['lang']))
{
/** @var CWebApplication $app */
$app = Yii::app();
$matches = array();
if ($app->user->hasState('__locale'))
$locale = $app->user->getState('__locale');
else if (preg_match('/^\/([\w_]+)\//', substr($app->request->requestUri, strlen($app->baseUrl)), $matches) !== false
&& isset($matches[1]) && in_array($matches[1], array_keys($app->cms->languages)))
$locale = $matches[1];
else
$locale = $app->cms->defaultLocale;
$params['lang'] = $app->language = $locale;
}
return parent::createUrl($route, $params, $ampersand);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment