UrlManager.php
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/**
* Url manager with automatic language param addition
*/
class UrlManager extends CUrlManager
{
/**
* Language key in request to be set
* @var string
*/
public $langKey = 'lang';
/**
* Constructs a URL, adding language param if it does not set.
* @param string $route the controller and the action (e.g. article/read)
* @param array $params list of GET parameters (name=>value). Both the name and value will be URL-encoded.
* If the name is '#', the corresponding value will be treated as an anchor
* and will be appended at the end of the URL.
* @param string $ampersand the token separating name-value pairs in the URL. Defaults to '&'.
* @return string the constructed URL
*/
public function createUrl($route, $params = array(), $ampersand = '&')
{
return parent::createUrl(
$route,
array_merge(array($this->langKey => Yii::app()->language), $params),
$ampersand);
}
public function parseUrl($request)
{
$res = parent::parseUrl($request);
// $app = Yii::app();
//
// if (isset($_GET['lang'])) { // set language from `lang` param
// if (count(array_intersect(array($_GET['lang']), Yii::app()->params['languages'])) == 0)
// $app->request->redirect('/');
// $app->language = $_GET['lang'];
// } else {
// $_GET['lang'] = Yii::app()->language;
// // todo: set language by request headers
// // $al = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
// }
return $res;
}
}