How do I replace a URL with "-" or "_"?

In YII If there is an empty space in the header that is used for a URL, then by default spaces are replaced with a "+" sign. Something like that:

www.domain.com/event/view/id/ Dj + Robag + Ruhme

What I want to do, I want to replace the + sign with a - - (a dash) or _ (an underscore). Something like that:

www.domain.com/event/view/id/ Dj-Robag-Ruhme

or

www.domain.com/event/view/id/ Dj_Robag_Ruhme

Now my urlManager:

'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'caseSensitive'=>false, 'rules'=>array( //'<controller:\w+>/<id:\d+>'=>'<controller>/view', //'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', //'<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), ), 
+4
source share
1 answer

Well, nothing strange, since Yii uses urlencode to encode URL parameters.

First approach

You can handle this in your model, for example.

 public function getUrl() { return Yii::app()->createUrl('/model/view', array( 'id'=>str_replace(' ', '-', $this->id), )); } 

Do not forget:

  • replace model with your model name,
  • use this method to get your model url,
  • change the action of your view in the controller:

     public actionView($id) { $id = str_replace('-', ' ', $id); // ..... } 

Second approach

You can use your own CUrlRule class:

http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes

+7
source

All Articles