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

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


Messages In This Thread
another dynamic routes topic - by El Forum - 03-08-2012, 03:52 PM
another dynamic routes topic - by El Forum - 03-08-2012, 04:16 PM
another dynamic routes topic - by El Forum - 03-08-2012, 04:23 PM
another dynamic routes topic - by El Forum - 03-08-2012, 04:40 PM
another dynamic routes topic - by El Forum - 07-06-2012, 08:14 AM
another dynamic routes topic - by El Forum - 07-06-2012, 09:14 AM
another dynamic routes topic - by El Forum - 07-06-2012, 10:02 AM
another dynamic routes topic - by El Forum - 07-06-2012, 10:57 AM



Theme © iAndrew 2016 - Forum software by © MyBB