Yii Framework Forum: urlManager rules for route aliases - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

urlManager rules for route aliases how to deal with dashes Rate Topic: -----

#1 User is offline   luoshiben 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 15-January 09

Posted 13 April 2010 - 01:17 PM

Hi All,

In order to better SEOize my front-end web app, I wanted to create aliases for some of my routes that use dashes to separate words. For example, I have a route /mymodule/mycontroller/flyingmonkeyswithguitars. However, I want search engines to find this page based on key words in the action name. Therefore, I want my app to use and display the route as /mymodule/mycontroller/flying-monkeys-with-guitars. How would I go about creating a urlManager rule that basically strips dashes from the action name before mapping the route? Thanks in advance for any help!
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 13 April 2010 - 01:26 PM

Are you not using path format in urlmanager config? You can do:

'rules' => array(
   'this/is/an-example' => 'module/controller/action',
),


Check out guide about friendly url's.
0

#3 User is offline   luoshiben 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 15-January 09

Posted 13 April 2010 - 03:04 PM

View PostY!!, on 13 April 2010 - 01:26 PM, said:

Are you not using path format in urlmanager config? You can do:

'rules' => array(
   'this/is/an-example' => 'module/controller/action',
),


Check out guide about friendly url's.


Thanks, Y!!. Yes, I am using urlFormat=>path, but I guess I was looking for something more dynamic. I could "hard code" each and every aliased route, but instead would rather create one rule that basically strips dashes from the action name and uses what is left as the mapped action ID. (So, /an-example maps to /anexample, and /heres-another-example maps to /heresanotherexample.) Is that possible with a rule? Thanks again.
0

#4 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 13 April 2010 - 03:18 PM

No this is not possible within the rules array.

If you really need this you can create a parent controller, override missingAction() and then run the correct action (with removed dashed). I guess you use creatUrl() somewhere in your application, so in order to create the correct url for an action, I guess you have to override createUrlRule() of the url manager.

But unless you don't have like hundred rules I would just put every rule into the config.
0

#5 User is offline   luoshiben 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 15-January 09

Posted 13 April 2010 - 03:43 PM

View PostY!!, on 13 April 2010 - 03:18 PM, said:

No this is not possible within the rules array.

If you really need this you can create a parent controller, override missingAction() and then run the correct action (with removed dashed). I guess you use creatUrl() somewhere in your application, so in order to create the correct url for an action, I guess you have to override createUrlRule() of the url manager.

But unless you don't have like hundred rules I would just put every rule into the config.


Thanks again for the reply. That makes sense. And you're right, its probably just eaiser to write out each rule for this with a relatively small web app. If/when the app gets larger I'll override my base Controller and CUrlManager.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users