Vote: I've Made A Core Change - Do You Like It?

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 :D

Hi

I am not into rules yet, so I am not ‘voting’.

However, regarding your last comment: as the code says ‘Yii::createComponent($rule)’, the class that is used for the rule instance is the class that is specified in ‘$rule’.

And ‘$rule’ is build from the ‘rules’ option. In ‘rules’, you can specify the class you want to use.

Something like this:


[size=2]    array([/size]

	     'class'=>'MyRuleClass',


             '<controller>/view',

             'pattern'          =>      '<controller:[a-z]+>',

);

which is so because of


[size=2]	[/size][size=2]protected function createUrlRule($route,$pattern)[/size]

	{

		if(is_array($route) && isset($route['class']))

			return $route;

		else

			return new $this->urlRuleClass($route,$pattern);

[size=2]	[/size][size=2]}[/size]



So - without looking into detail though - I think you could do it as an extension.

Even so, why not be in core?

Hi

I understand your comment; I’ve also made some suggestions to make modifications to the core.

It is difficult to convince to make a functional core modification with any kind of system Therefore it is less effort and more efficient to accept to do it with existing methods.

Despite that rule of thumb, I have 4 core modifications I make on each upgrade of Yii I’ld prefer to see “fixed” in Yii.