Do I use POST variables or routed vars in URL? |
[eluser]luismartin[/eluser]
I'm implementing a form to search for products. The variables to be managed are like these: "product_type", "product_collection" (two nested levels of categories), "search_term", "offset", "per_page" (the last ones to manage pagination). I'm not sure how to do it: whether to send them through POST, which means same URL for different results (this might be SEO unfriendly), or through routed variables as segments in the URL, which in turn would act like parameters in the controller. This one looks rather ugly to me, because if this is the URL structure: products/display/[product_type]/[product_collection]/[search_term]/[offset]/[per_page] what if I only want to filter by term to search? I use to do something like: products/display/0/0/term-to-search What do you think is a better approach to this question?
[eluser]Andreas Bergström[/eluser]
Definitely search parameters in URL for so many reasons. Just use associative parameters and simply leave out unused filters, then parse it with uri->uri_to_assoc(n).
[eluser]luismartin[/eluser]
[quote author="Andreas Bergström" date="1338971681"]Definitely search parameters in URL for so many reasons. Just use associative parameters and simply leave out unused filters, then parse it with uri->uri_to_assoc(n).[/quote] Hello Andreas, thanks for your reply! Seems that I missed part of the documentation and I didn't follow to the letter CI's convention for a URI structure. Instead of doing this: controller/method/key1/value1/key2/value2/key3/value3 I directly did this: controller/method/value1/value2/value3 thus forcing all the parameters to have a fixed position, even when some of them were not defined, in which case I had to add a 0 (no self-explanatory at all) to those segments. :-S Thanks for pointing me to the right direction! :-)
[eluser]luismartin[/eluser]
Just one question I forgot to ask: If I use your approach, would you recommend to declare the controller method without any given parameters?? I mean, if I declared it this way: Code: public function mymethod($key1, $value1, $key2, $value2) { and in some cases I only needed $key2, in order to avoid something like mymethod/key1/0/key2/value2 would it be better to just do this?: Code: public function mymethod() {
[eluser]Andreas Bergström[/eluser]
Go for the second one. ![]() In my opinion, passing routing-matches as variables to controllers makes code less readable and tangles the flow.
[eluser]luismartin[/eluser]
[quote author="Andreas Bergström" date="1338985551"]Go for the second one. ![]() In my opinion, passing routing-matches as variables to controllers makes code less readable and tangles the flow.[/quote] ok, thanks again for your tip! That's very flexible to program. The only drawback I see is that it might cause some search optimization problems, because different URL's would have the same content. Nothing serious I think anyway. Cheers!
[eluser]Andreas Bergström[/eluser]
[quote author="luismartin" date="1338989238"] That's very flexible to program. The only drawback I see is that it might cause some search optimization problems, because different URL's would have the same content. Nothing serious I think anyway.[/quote] Just use the canonical-tag to specify the most general of them. ![]() |
Welcome Guest, Not a member yet? Register Sign In |