UrlManager.php 1.58 KB
<?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;
    }

}