【问题标题】:How to Rewrite hardcoded url in yii?如何在 yii 中重写硬编码的 url?
【发布时间】:2013-10-17 22:45:41
【问题描述】:

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

我做了一个url如下,

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

ie:我需要从 url 中删除 hospital?name= 和 &id=14#ad-image-0...

有人可以帮忙吗?

【问题讨论】:

标签: 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 2022-10-31
      相关资源
      最近更新 更多