Created
May 7, 2012 19:08
-
-
Save crisu83/2629751 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* CmsUrlManager class file. | |
* @author Christoffer Niska <christoffer.niska@nordsoftware.com> | |
* @copyright Copyright © 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