Urlmanager Having Different Controller/action With Same Number Of Arguments

Hi,

I’m facing a problem as below:

In my config/main.php, I have

‘<lang>/<cnty:.*?>’=>’abc/index’,

‘<lang>/<airline:.*?>’=>’xyz/view’,

These are two different controller/actions with different parameter names. However, it always defaults to the first rule, whatever it may be. Is there a way to tell Yii how to differentiate between the two rules based on same number of parameters/arguments but different parameter names? Or does it HAVE to be different number of parameters for each controller/action?

Your advice and help will be much appreciated.

Thanks,

Omar

Hi Omar, welcome to the forum.

It’s up to the regular expressions that you use for the url manager rules.

If the given url falls into the 1st pattern, then it will be routed to the 1st route, without checking the 2nd rule and its pattern. So, try to come up with the pattern that is only applicable to "country" or "airline", then you can solve the problem.

Hi softark,

Thank you for your advice. I’m not very good at regular expressions so can you please help me a bit further and suggest a regular expression that could work. Both my controller/actions expect, for example, United States for abc/index and Air Blue for xyz/view. One argument for both actionIndex and actionView functions in different controllers.

Thanks & Kind Regards,

Omar

??

I’m sorry, but I don’t get what you mean by the sample.

How do you know ‘abc’ stands for ‘United States’ and ‘xyz’ stands for ‘Air Blue’?

Hi,

Sorry about the confusion. Here is my URLManager:

‘urlManager’=>array(

		'showScriptName'=&gt;false,


		'urlFormat'=&gt;'path',


		'rules'=&gt;array(


			'&lt;lang&gt;'=&gt;array('site/index', 'urlSuffix'=&gt;''),


			'&lt;lang&gt;/site/&lt;view:(privacy)&gt;' =&gt; 'site/page',


			'&lt;lang&gt;/site/contact' =&gt; 'site/contact',





			'&lt;lang&gt;/&lt;airline:.*?&gt;'=&gt;'airlines/view',


			'&lt;lang&gt;/&lt;cnty:.*?&gt;'=&gt;'country/index',





			'&lt;controller:&#092;w+&gt;/&lt;id:&#092;d+&gt;'=&gt;'&lt;controller&gt;/view',


			'&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;/&lt;id:&#092;d+&gt;'=&gt;'&lt;controller&gt;/&lt;action&gt;',


			'&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;'=&gt;'&lt;controller&gt;/&lt;action&gt;',


		),


	),

For URLs,

www.url.com/tr/United+States

and

www.url.com/tr/Air+Blue

only the first rule is used by Yii, breaking the other controller/action.

Both ‘<lang>/<airline:.?>’ and '<lang>/<cnty:.?>’ have exactly the same regular expression pattern, so ‘country/index’ will never have the chance to be applied.

For example, if you are sure that all the air lines have ‘Air’ or ‘Aero’ in their names, then you could do something like this:




	'<lang>/<airline:.*(Air|Aero).*?>'=>'airlines/view',

	'<lang>/<cnty:.*?>'=>'country/index',



But probably it’s better to adopt a simple solution:




	'<lang>/airline/<airline:.*?>'=>'airlines/view',

	'<lang>/country/<cnty:.*?>'=>'country/index',



www.url.com/tr/country/United+States

and

www.url.com/tr/airline/Air+Blue

Hi softark,

Thank you very much for your help. I have implemented your suggestion after getting approval from my client and everything is now working well. :)

Thanks again.

Kind Regards,

Muhammad Omar