another dynamic routes topic |
[eluser]Unknown[/eluser]
Hello everybody. I'm writing an application using the latest CI version. My goal is to serve some dynamic routes after everything else. I tried to make regexp routes and it worked, but i had to write all the present controllers before the regexp, like this: Code: $route['default_controller'] = "organizations"; Is there any way to accomplish it more dynamic, something more efficient? I found this http://ellislab.com/forums/viewthread/135105/#666746 There is described a way to change the behavior of the controller lookup methods, but i can't fit my dynamic issue with it. Any help? Thanks in advance.
[eluser]InsiteFX[/eluser]
The order of your routes is very important! Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
[eluser]Unknown[/eluser]
Thanks for the reply. Yes, but the problem is if I remove all but the last 2 lines, It sends everyting to the articles/$1 and articles/catAndArticleName/$1/$2 and messes all other controllers. I mean if there are only these two lines, if I reach the url.ltd/news or url.ltd/news/details/1 it throws everyting to the articles controller. So every time I need new controller I have to put it before these two lines if code in the routes.php. Is there any way to make it easier and more dynamic (without the need of putting Code: $route['controller_name'] = "controller_name"; Code: $route['([a-zA-z_]+)'] = "articles/cat/$1";
[eluser]juliano.ma[/eluser]
I've needed something like that. I solved this way: in your application/config/routes.php Code: // default routes in application/controllers/my_router.php Code: class My_router extends CI_Controller {
[eluser]jakelehner[/eluser]
Are you only wanting to use regex for urls that start with 'articles/'? If so, you can write your regex to only apply to those requests rather than everything.
[eluser]juliano.ma[/eluser]
[quote author="jakelehner" date="1341591276"]Are you only wanting to use regex for urls that start with 'articles/'? If so, you can write your regex to only apply to those requests rather than everything.[/quote] In my example, any URL that is different from the routes generated, redirects to My_router. Except the URL's to the default routes. This helps when you want the title of an article in the first segment. example: mysiteurl.com/my-blog-title-post In the controller My_router you can search for "my-blog-post-title" in the database. If not, go to 404 or any page you want.
[eluser]jakelehner[/eluser]
I see. So really what you need to do is get a dynamic list of all your controllers, and build routes based on them? Kind of a 'brute force' approach ... but maybe you could use the file helper to get a file listing of everything under your controllers directory, then loop through and build your route array dynamically. If the CI helpers aren't available, the same could be accomplished fairly easily with native PHP. |
Welcome Guest, Not a member yet? Register Sign In |