Help With Understanding Url Rules

My requirement:

  • module/controller/key (string of 32 characters, numbers+letters)------This should go to the index method of this controller that is defined like "public function actionIndex($key)"

  • module/controller/action------This should route normally to appropriate method of this controller

  • module------This should go to the default module controller and to its index method

My current rule set:


'<module:\w+>/<controller:\w+>'=>'<module>/<controller>/index',

'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',

'<module:\w+>/<controller:\w+>/<key:\w+>'=>'<module>/<controller>/index',

'<module:\w+>/<action:\w+>'=>'<module>/default/<action>',

'<module:\w+>'=>'<module>/default/index'

If I interchange rule 2 and 3, my 1st requirement works but any method name is also sensed as a get parameter and sent to the index. If the rules remain the same my 1st requirement doesn’t work.

It’s confusing! :blink:

If the length is guaranteed to be 32 characters and there are no other parameters, you could try this:




'<module:\w+>/<controller:\w+>'=>'<module>/<controller>/index',

'<module:\w+>/<controller:\w+>/<key:\w{32}^>'=>'<module>/<controller>/index',

'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',

'<module:\w+>/<action:\w+>'=>'<module>/default/<action>',

'<module:\w+>'=>'<module>/default/index'



I’m not sure whether the ^ character to denote the end of the string is required. It depends how Yii parses the rules.

Yup that works, so my method name should be less than 32 characters, right?

Now my rules look like:


'<module:\w+>/<action:\w+>'=>'<module>/default/<action>',

'<module:\w+>/<controller:\w+>'=>'<module>/<controller>/index',

'<module:\w+>/<controller:\w+>/<key:\w{32}>'=>'<module>/<controller>/index',

'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',

'<module:\w+>'=>'<module>/default/index',

In this case:

module/controller/action -------- Works! But,

module/action ------------------- Doesn’t work. This should go to the default controllers index method.