views:

1247

answers:

1

I need a codeigniter route so all of the following urls:

admin/users/page/:num
admin/accounts/page/:num
members/results/page/:num
products/page/:num

are forwarded to

admin/users/index
admin/accounts/index
members/results/index
products/index

respectively. I'd like just one regexp which could do the trick rather than me setting the routes manually each time.

To be specific, any url which ends in page/:num should be forwarded to its respective controller's index method. And by :num I mean any number.

Is this possible?

+3  A: 

I don't really get why you would want to do that. (I assume that you want to get the page number from the URL instead)

add these lines to your system/application/config/routes.php (couldn't think of a one-line solution) :

$route['([a-z]+)/page/:num'] = "$1/index";
$route['([a-z]+)/([a-z]+)/page/:num'] = "$1/$2/index";

cmiiw.

andyk
Thanks, works like a charm! The reason I wanted this is because my custom pagination library creates links like /path/to/controller/page/$page. And I don't want to manually set up each route. Is there a better way to do this?
Click Upvote
Not if you use the index method of the controller. If you use a method like `search`, then `/users/search/page/7` would get translated to class "Users", method "search" and parameters "page" and 7.
Boldewyn
might want to change how your current pagination library works, because this way, you'd be constrained to using that one method you specify at the config (in this case, index).. and yes, I know that CI's default pagination is not favored sometimes.
andyk
i'm happy with it. Conventions over configuration as per 37 signals :)
Click Upvote
Believe it or not, your comment has just helped me to get over this roadblock I'm currently facing. Apparently I like to overthink too much. Thanks
andyk
What was the roadblock?
Click Upvote
I was writing for a function (in CI too), basically I'm looking for ways to enhance it more, so that it could accept a parameter for (this should be in caps) whatever situation in the future it might come across. This has been going for hours, then I figure, "what the heck, if I ever need something, I'll just come back and code it", and left.
andyk
haha, i do that as well. If its something really complicated i suggest writing a class for handling it rather than just a function. E.g class MyComplicatedStuffHandler rather than function handleMyComplicatedStuff().
Click Upvote