URI Routing, better way to do that - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: URI Routing, better way to do that (/showthread.php?tid=15702) |
URI Routing, better way to do that - El Forum - 02-12-2009 [eluser]sybrex[/eluser] Hello! I have such url: http://mysite.com/region/category/subcategory/new/page_n Where all these segments are optional which makes a lot of variants like: Code: http://mysite.com/ Region is a string like 'street1', 'street2'.. There are only two categories: 'events', 'places'; Subcategory is a string like 'fun', 'sport'.. New is also a string and can be: 'new' or '' Page_n is a numeric value I just want to know if it is the best way to make a controller like this: Code: class Postlist extends Controller and to write rules to all of the variants? routes.php: Code: $route['(:any)/(events|places)/(:any)/(new|)/(:num)'] = 'postlist/posts/$1/$2/$3/$4/$5'; URI Routing, better way to do that - El Forum - 02-12-2009 [eluser]pistolPete[/eluser] I suggest using a route like that: Code: $route[':any'] = "postlist"; Code: $this->uri->segment_array() Have a look at the user guide. URI Routing, better way to do that - El Forum - 02-12-2009 [eluser]Phil Sturgeon[/eluser] I'd use an associative array, which is explained in the URI class manual. Then you can do: Code: class Postlist extends Controller You could extract() args too to get use variables instead of array keys. URI Routing, better way to do that - El Forum - 02-12-2009 [eluser]sybrex[/eluser] [quote author="pyromaniac" date="1234462923"]I'd use an associative array, which is explained in the URI class manual. [/quote] Yes, it is a good way, but as far as I understand my url from this: http://mysite.com/region/category/subcategory/new/page_n must transform to this: http://mysite.com/region/region_name/category/category_name/subcategory/subcategory_name/new/new_or_not/page/page_n and it is not very nice for me P.S. Working now on _remap() function, not so easy as I thought at first, many variants URI Routing, better way to do that - El Forum - 02-12-2009 [eluser]xwero[/eluser] If the url structure is still being discussed maybe you better create a category/region/subcategory structure. Then you only have to use your last route. Having a catch all first segment should be avoided because all controllers that are not defined in the routes will route to the catch all route. Your routes are too specific Code: $route['(events|places)/(:any)/(new|)/(:num)'] = 'postlist/posts/$1/$2/$3/$4'; Code: $route['(events|places)(.*)'] = 'postlist/posts/$1$2'; and then you controller method can look like this Code: function posts() URI Routing, better way to do that - El Forum - 02-12-2009 [eluser]sybrex[/eluser] [quote author="xwero" date="1234470597"]If the url structure is still being discussed maybe you better create a category/region/subcategory structure.[/quote] Agree with you, but have to go with the old structure now. Well, finally I wrote the _remap() function: Code: function _remap() It works fine, but I begin thinking about my first way of solving the problem again... |