Guru Ananth
Guru Ananth

Reputation: 1

How to Rewrite hardcoded url in yii?

How to Rewrite hardcoded url in yii?

I have made a url as follows,

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

It redireceted to the url as follow, http://abc.com/hospital?name=yyy&id=14#ad-image-0 I Just want the url to be as follows, http://abc.com/yyy

ie: i need to remove hospital?name= and &id=14#ad-image-0 from the url...

Can any one help?

Upvotes: 0

Views: 215

Answers (2)

user2869049
user2869049

Reputation: 129

if you want to make a user friendly and seo-friendly like http://abc.com/hospital/yyy

You can use this:

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
    }
}

More informatino there ! http://www.yiiframework.com/doc/guide/1.1/fr/topics.url#using-custom-url-rule-classes

Upvotes: 0

Samuel Liew
Samuel Liew

Reputation: 79032

In your urlManager rules, add this after all other rules:

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

assuming view is the action you want to call - replace it with your action name

Then your link as follows:

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

Upvotes: 1

Related Questions