CodeIgniter Forums

Full Version: a better way to handle custom routes?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

I've already asked the question in this thread but i guess it got lost in the opinion responses so i though i ask it again as a purely technical question.

In the same thread a response was most of the frameworks use regex for custom routing. What is their solution? And what can be gained by using that solution.

El Forum

[eluser]GSV Sleeper Service[/eluser]
here's something I started working on a while back, I've abandoned it since, seeing as though CI provides pretty much everything I need.

$app_root = "/dev/test";

$pages = Array(
    '#^/(?:index\.(?:html|php))?$#'                           => 'page-index.php',       # main page handler
    '#^/brand/(\w+)?(?:/(\w+))?(?:/(\w+))?(?:/page(\d+))?#' => 'page-brand.php',       # brand handler
    '#^/item/([a-zA-Z0-9-]+)#'                                 => 'page-item.php',        # single item handler
    '#^/login.html#'                                      => 'page-login.php',       # login page handler
    '#^/register.html#'                                   => 'page-register.php',    # registration page handler
    '#^/logout.html#'                                     => 'page-logout.php'       # logout page handler

$parts = parse_url($_SERVER['REQUEST_URI']);
$path = preg_replace('#/+#', '/', $parts['path']); // drop multiple slashes
$path = str_replace($app_root,"",$path); // remove app_root from path

foreach ($pages as $page_pattern => $page) {
    echo $page_pattern.'<br />';
    if (preg_match($page_pattern, $path, $matches)) {
        echo "page match = $page<br />";
        //require $page;

El Forum

Thanks for responding GSV Sleeper Service but the question was how to do custom routing without the use of using regular expressions.

As stated in the other thread if you have a substantial number of custom routes it can weigh on the application. But i'm wondering how many routes you would need to slow the application down?