Как говорится надо, значит сделаем!
Покритикуйте такой подход для решения задачи
В конфиге указываем
Код: Выделить всё
'urlManager'=> array(
'class'=>'application.components.EUrlManager',
'urlFormat'=>'param',
'urlSuffix'=>'.html',
'showScriptName'=>false,
'rules'=>array(
'test/<tid:\d+>'=>'home/index',
),
)
Далее наследуемся от CUrlManager и переопределяем/добавляем
Код: Выделить всё
<?php
class EUrlManager extends CUrlManager
{
const PARAM_FORMAT='param';
private $_urlFormat=self::GET_FORMAT;
public $rules=array();
private $_rules=array();
public function init()
{
parent::init();
$this->processRules();
}
protected function processRules()
{
if(empty($this->rules) || $this->getUrlFormat()===self::GET_FORMAT)
return;
if($this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
{
$hash=md5(serialize($this->rules));
if(($data=$cache->get(self::CACHE_KEY))!==false && isset($data[1]) && $data[1]===$hash)
{
$this->_rules=$data[0];
return;
}
}
foreach($this->rules as $pattern=>$route)
$this->_rules[]=$this->createUrlRule($route,$pattern);
if(isset($cache))
$cache->set(self::CACHE_KEY,array($this->_rules,$hash));
}
protected function createUrlDefault($route,$params,$ampersand)
{
if($this->getUrlFormat()===self::PARAM_FORMAT)
{
$url=$this->getBaseUrl();
if(!$this->showScriptName)
$url.='/';
if($route!=='')
{
$url.='?'.$this->routeVar.'='.$route.$this->urlSuffix;
if(($query=$this->createPathInfo($params,'=',$ampersand))!=='')
$url.=$ampersand.$query;
}
else if(($query=$this->createPathInfo($params,'=',$ampersand))!=='')
$url.='?'.$query;
return $url;
}
else
return parent::createUrlDefault($route,$params,$ampersand);
}
public function parseUrl($request)
{
if($this->getUrlFormat()===self::PARAM_FORMAT)
{
if(isset($_GET[$this->routeVar]))
{
$rawPathInfo = $_GET[$this->routeVar];
$pathInfo=$this->removeUrlSuffix($rawPathInfo,$this->urlSuffix);
foreach($this->_rules as $i=>$rule)
{
if(is_array($rule))
$this->_rules[$i]=$rule=Yii::createComponent($rule);
if(($r=$rule->parseUrl($this,$request,$pathInfo,$rawPathInfo))!==false)
return $r;
}
if($this->useStrictParsing)
throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
array('{route}'=>$pathInfo)));
else
return $pathInfo;
}
else
return '';
}
else
return parent::parseUrl($request);
}
public function getUrlFormat()
{
return $this->_urlFormat;
}
public function setUrlFormat($value)
{
if($value===self::PATH_FORMAT || $value===self::GET_FORMAT || $value===self::PARAM_FORMAT )
$this->_urlFormat=$value;
else
throw new CException(Yii::t('yii','CUrlManager.UrlFormat must be either "path", "get" or "param".'));
}
}
В результате можем использовать
Код: Выделить всё
http://domain.loc/r=test/about //'urlFormat'=>'param',
http://domain.loc/test/about // 'urlFormat'=>'path',