Url Manager Rules With Optional Params

Hello, I really need some help in creating some URL rules.

I have a route : product/list, where I show a list of models, let’s say Products (think notebooks), that can be filtered. So, having Brand and Memory size as filters, my URL would look like :


 http://www.mywebsite.com/product/list?brand=1,2&memory=8,16&order=price asc

Shows all products that have brand IDs 1 and 2 and memory size 8 and 16. As you can see, I also added an order by param. All these filters are dynamic, so one time, I could have only the memory param, or only the order param, or 2 of these.

So, one way to make URL rules would be to write multiple rules, but that means I have to write a rule for each combination of params.


 

  'products/<brand:\d+>/<memory:\d+>' => 'product/index'  //for brand and memory size

   'products/<brand:\d+>' => 'product/index'  //for brand only

 

//... and so on, for every possible combination, gets more complicated by having more than 3 params.

 

Anyway it can be simpler than this ? Or, if I do write all the rules, will there by severe performance problems ?

I’m looking for a way of writing just one rule, that can take params that might be there or not.

I found this, but it is not working http://www.yiiframework.com/forum/index.php/topic/35616-yii-url-rules-optional-parameters/

PS : I know that my regex don’t work right now, cause \d+ only matches decimals, I will have to look into how to match comma separated id’s.

From the guide:

So, I think you can create the route like this:




'products/*' => 'product/index'



That rule will accept url like “products/brand/1,2/memory/8,16/order/price” and any combination of parameters (according to the guide, I haven’t tried it).

That does work as you said, but I cannot modify the appearance of the URL. I’m stuck with the brand, memory params in there. I would like to take these out, or change their name, based on my app’s current language.

If you don’t specify the parameter’s name, how the program knows what parameters being passed by user?

Eg. A user enters this url:




http://yoursite.com/products/1,2/3,4/8,16



Which one is parameter for brand or memory or else? Unless you determine it by the order of parameters, but you said user can also enter parameter for memory only, so it will be:




http://yoursite.com/products/8,16



What is 8,16? memory size or brand id?

Sorry if it’s not a direct answer to your issue, but maybe it’s worth to think about.

Well that is why we have Url Manager. Url manager knows which parameter is which, but I have to write a bunch of rules :). Optional params in Url rules do seem pretty impossible, but I had to ask.

I wouldn’t take out the params through the pattern, but change their name based on my current language.