如何在yii中重写硬编码的URL?

时间:2013-10-10 08:54:50

标签: url yii url-rewriting hardcoded

如何在yii中重写硬编码的网址?

我已经制作了如下网址,

CHtml::link($visit->health_institute_name, array('hospitals_single', 'id'=>$visit->health_institute_id));

它重定向到网址如下, http://abc.com/hospital?name=yyy&id=14#ad-image-0  我只想要网址如下, http://abc.com/yyy

即:我需要从网址中删除医院?name =和& id = 14#ad-image-0 ...

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

在您的urlManager规则中,在所有其他规则之后添加:

'<name>' => 'hospital/view',

假设view是您要调用的操作 - 请将其替换为您的操作名称

然后您的链接如下:

CHtml::link($visit->health_institute_name, Yii::app()->getBaseUrl(true).'/'.$visit->name);

答案 1 :(得分:0)

如果您希望提高用户友好性和友好性,例如 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