So, backstory:
I am really tired of doing routes like this. Making two rules is 2x the parsing of one rule and defaults should be something any mature framework should have. I can’t figure out how to do this and I’ve torn the docs apart.
defaultParams => GET
'rules'=>array(
array(
'<controller>/view',
'pattern' => '<controller:[a-z]+>',
),
array(
'<controller>/<action>',
'pattern' => '<controller:[a-z]+>/<action>',
),
),
I’ve gone ahead and written this in about ~10 lines of change to the core. CUrlRule.
'rules'=>array(
array(
'<controller>/<action>',
'pattern' => '(<controller:[a-z]+>)?(/<action:[a-z]>)?',
'routeDefaults' => array(
'<action>' => 'default',
'<controller>' => 'index',
)
),
),
This solves: your homepage route, your controller only route, and your controller/action route.
This is a super basic example.
Here is my use case example
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
array(
'<gpmodule>/<pmodule>/<module>/<controller>/<action>',
'pattern' => '(<gpmodule:[a-z]+>)?(/<pmodule:[a-z]+>)?(/<module:[a-z]+>)?(/<controller:[a-z]+>)?(/<action:[a-z]+>)?(/<id:[0-9]+>)?',
'routeDefaults' => array(
'<action>' => 'view',
'<controller>' => 'default',
'<gmodule>' => '',
'<pmodule>' => '',
'<module>' => '',
)
),
array(
'<gpmodule>/<pmodule>/<module>/<controller>/<action>',
'pattern' => '(<gpmodule:[a-z]+>)?(/<pmodule:[a-z]+>)?(/<module:[a-z]+>)?(/<controller:[a-z]+>)?/<id:[0-9]+>(/<action:[a-z]+>)?',
'routeDefaults' => array(
'<action>' => 'view',
'<controller>' => 'default',
'<gmodule>' => '',
'<pmodule>' => '',
'<module>' => '',
)
),
),
),
0,1,2,3 modules deep I can do the following
/manage/crm/match => /manage/crm/match/default/default
/manage/crm/match/34 => /manage/crm/match/default/view/ => $id = 34
/manage/crm/match/34/view => /manage/crm/match/default/view/ => $id = 34
/manage/crm/match/view/34 => /manage/crm/match/default/view/ => $id = 34
/manage/crm/match/default/view/34 => /manage/crm/match/default/view/ => $id = 34
/manage/crm/34 => /manage/crm/default/view/ => $id = 34
/manage/34 => /manage/default/view/ => $id = 34
/34 => /default/view/ => $id = 34
basically in two lines that can be accomplished.
Why isn’t this an extension? because of this line that discriminates against custom url rules:
protected function createUrlRule($route,$pattern)
{
if(is_array($route) && isset($route['class']))
return $route;
else
return new $this->urlRuleClass($route,$pattern);
}
I can’t override constructor, because it’s done with
$this->_rules[$i]=$rule=Yii::createComponent($rule);
AND NOT
return new $this->urlRuleClass($route,$pattern);
Thanks