【问题标题】:How to Rewrite hardcoded url in yii?如何在 yii 中重写硬编码的 url?
【发布时间】:2013-10-17 22:45:41
【问题描述】:
【问题讨论】:
标签:
url
yii
url-rewriting
hardcoded
【解决方案1】:
在您的 urlManager 规则中,在所有其他规则之后添加:
'<name>' => 'hospital/view',
假设 view 是您要调用的操作 - 将其替换为您的操作名称
那么你的链接如下:
CHtml::link($visit->health_institute_name, Yii::app()->getBaseUrl(true).'/'.$visit->name);
【解决方案2】:
如果您想让用户友好和对 seo 友好,例如 http://abc.com/hospital/yyy
你可以用这个:
class CarUrlRule extends CBaseUrlRule
{
public $connectionID = 'db';
public function createUrl($manager,$route,$params,$ampersand)
{
if ($route==='car/index')
{
if (isset($params['manufacturer'], $params['model']))
return $params['manufacturer'] . '/' . $params['model'];
else if (isset($params['manufacturer']))
return $params['manufacturer'];
}
return false; // this rule does not apply
}
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches))
{
// check $matches[1] and $matches[3] to see
// if they match a manufacturer and a model in the database
// If so, set $_GET['manufacturer'] and/or $_GET['model']
// and return 'car/index'
}
return false; // this rule does not apply
}
}
那里有更多信息!
http://www.yiiframework.com/doc/guide/1.1/fr/topics.url#using-custom-url-rule-classes