CodeIgniter Forums
another dynamic routes topic - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: another dynamic routes topic (/thread-49953.html)



another dynamic routes topic - El Forum - 03-08-2012

[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";
$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.


another dynamic routes topic - El Forum - 03-08-2012

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



another dynamic routes topic - El Forum - 03-08-2012

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


another dynamic routes topic - El Forum - 03-08-2012

[eluser]InsiteFX[/eluser]
URI Class

Controllers - See _remap function



another dynamic routes topic - El Forum - 07-06-2012

[eluser]juliano.ma[/eluser]
I've needed something like that.

I solved this way:

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

// array class controlers (first segments)
$clist = array(
                    'auth',
                    'panel',
                    'admin',
                    'my_other_controller'
               );

// 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
Code:
class My_router extends CI_Controller {

    function __construct()
    {
        parent::__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...
        }
        else
        {
            echo $this->uri->segment(1);

            do that...
        }
    }
}



another dynamic routes topic - El Forum - 07-06-2012

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


another dynamic routes topic - El Forum - 07-06-2012

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


another dynamic routes topic - El Forum - 07-06-2012

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