Welcome Guest, Not a member yet? Register   Sign In
another dynamic routes topic

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:
$route['default_controller'] = "organizations";
$route['mobile'] = "mobile/welcome";
$route['mobile/(:any)'] = "mobile/$1";
$route['404_override'] = '';
$route['projects'] = "projects";
$route['projects/(:any)'] = "projects/$1";
$route['news'] = "news";
$route['news/(:any)'] = "news/$1";
$route['([a-zA-z_]+)'] = "articles/cat/$1";
$route['([a-zA-z_]+)/(:any)'] = "articles/catAndArticleName/$1/$2";
That works correct, but it needs to populate it every time I add new controller.
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.

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.

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
$route['controller_name'] = "controller_name";
$route['controller_name/(:any)'] = "controller_name/$1";
for each controller) and still have the ability to make the
$route['([a-zA-z_]+)'] = "articles/cat/$1";
$route['([a-zA-z_]+)/(:any)'] = "articles/catAndArticleName/$1/$2";
Thanks in advance.

URI Class

Controllers - See _remap function

I've needed something like that.

I solved this way:

in your application/config/routes.php
// default routes
$route['default_controller'] = "welcome";
$route['404_override'] = 'my_404';

// array class controlers (first segments)
$clist = array(

// Number of segments you want
$num_seg = 5;

// Iteration for each value of the array
foreach ( $clist as $cname )
    $route[$cname] = $cname;

    $a = "/(.*)"; // regexp to any character in segments
    $b = "/$";
    $c = "";
    $d = "";

    for($i = 1; $i <= $num_seg; $i++)
        $c .= $a;
        $d .= $b.$i;

        $route[$cname.$c] = $cname.$d;

// my router controller
$route['(:any)'] = "my_router";

in application/controllers/my_router.php
class My_router extends CI_Controller {

    function __construct()

    function index()
        // here you can do many things as you need.

        // Note: Class URI is initialized automatically by the system so there is no need to do it manually.

        if($this->uri->total_segments() > 1)
            echo "<pre>";
            print_r( $this->uri->segment_array(  ) );
            echo "</pre>";
            do this...
            echo $this->uri->segment(1);

            do that...

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 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.


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.

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.

Theme © iAndrew 2016 - Forum software by © MyBB