Welcome Guest, Not a member yet? Register   Sign In
Dynamic Route to Specific Controller
#7

[eluser]solepixel[/eluser]
Ok, my routes are generated dynamically. The code that does it is here:
Code:
list($route,$qs) = (strpos($_SERVER['REQUEST_URI'], '?') !== false) ? explode('?', $_SERVER['REQUEST_URI']) : array($_SERVER['REQUEST_URI'],'');
$uri = $route;
if($uri != '/'){
    $uri = trim($uri, '/');

    $route = trim($route, '/');
    $route = str_replace('-', '_', $route);
}
$path = ($row->controller == '/') ? 'index_controller' : trim($row->controller, '/');

$route_uri = trim($row->uri,'/').'/(.*)';
$DB_ROUTES[$route_uri] = $path.'/$1';

$DB_ROUTES[$route] = $path;

$row is my row from the DB, controller specifies which controller to use for the request, and uri is the customized uri in the CMS and $DB_ROUTES is an array that is merged with the $routes array in the routes.php config file.

The output for the URI /reviews/test/write/65/ is:
Code:
Array
(
    [reviews/test/(.*)] => reviews/$1
    [reviews/test/write/65] => reviews
)

reviews/test is the customized URI, reviews is the controller to handle the request.

Inside my reviews controller, here's the top of the index method:
Code:
function index($uri2='', $uri3='')
{
    echo 'uri2: '.$uri2.'<br />';
    echo 'uri3: '.$uri3.'<br />';
    echo $this->uri->rsegment(1) . ' vs. ' . $this->uri->segment(1).'<br />';
    echo $this->uri->rsegment(2) . ' vs. ' . $this->uri->segment(2).'<br />';
    echo $this->uri->rsegment(3) . ' vs. ' . $this->uri->segment(3).'<br />';
    echo $this->uri->rsegment(4) . ' vs. ' . $this->uri->segment(4).'<br />';
    exit();

This code outputs:
Code:
uri2:
uri3:
reviews vs. reviews
index vs. test
vs. write
vs. 65

As you can see, the URL variables give me nothing when passed into the method. The rsegments only give me the the hard coded routes and segments give me what I expect. So maybe I can hack up the rsegments method and maybe pass a 2nd paramater that gives the ROUTED uri segment, which is what I expect it to do in the first place. I figured it would work like the way CI handles uri segments. If your applications starts at /my/application/, then uri segments start AFTER /my/application/ so uri->segment(1) would return '' instead of 'my'.

Anyway, thanks for helping, i'm definitely open to suggestions. I'm now determined to figure this out.

On a side note, i had a terrible time typing This without $this - Thanks CI!


Messages In This Thread
Dynamic Route to Specific Controller - by El Forum - 06-30-2010, 12:55 PM
Dynamic Route to Specific Controller - by El Forum - 06-30-2010, 01:34 PM
Dynamic Route to Specific Controller - by El Forum - 06-30-2010, 02:08 PM
Dynamic Route to Specific Controller - by El Forum - 06-30-2010, 04:05 PM
Dynamic Route to Specific Controller - by El Forum - 06-30-2010, 05:18 PM
Dynamic Route to Specific Controller - by El Forum - 06-30-2010, 09:10 PM
Dynamic Route to Specific Controller - by El Forum - 07-01-2010, 07:45 AM
Dynamic Route to Specific Controller - by El Forum - 07-01-2010, 07:57 AM
Dynamic Route to Specific Controller - by El Forum - 07-01-2010, 08:10 AM
Dynamic Route to Specific Controller - by El Forum - 07-01-2010, 08:23 AM
Dynamic Route to Specific Controller - by El Forum - 07-01-2010, 10:16 AM
Dynamic Route to Specific Controller - by El Forum - 07-01-2010, 10:27 PM



Theme © iAndrew 2016 - Forum software by © MyBB