Easiest way to map route in PHP

I was browsing Symfony's website. I didn't really feel like I need all the functionality the framework offers, but I did like the routing part. It allows you to specify URL pattern like this

/some/path/{info}

Now for URL like www.somewebsite.com/app.php/some/path/ANYTHING it would allow you to send client a response specific for this URL. You could also use string ANYTHING and use it similar as GET parameter. There is also option to hide app.php part of the URL which leaves us URL like www.somewebsite.com/some/path/ANYTHING. My question is what's best approach to do this without a complex framework?

Answers


I recommend this article http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/ to understand url rewrite using apache mod_rewrite you do not need any framework just php. Also this is what in the depth any framework implements


I have made my own mini-framework with the same routing syntax. Here's what I do:

  1. Use MOD_REWRITE to store the parameters (like /some/path/{info}) in a $_GET variable I call params:

    RewriteRule ^(.+)(\?.+)?$ index.php?params=$1 [L,QSA]

  2. Parse the parameters and store them globally using this function:

    public static function parseAndGetParams() {

    // get the original query string
    
    $params = !empty($_GET['params']) ? $_GET['params'] : false;
    
    // if there are no params, set to false and return
    if(empty($params)) {
        return false;
    }
    
    // append '/' if none found
    if(strrpos($params, '/') === false) $params .= '/';
    
    $params = explode('/', $params);
    // take out the empty element at the end
    if(empty($params[count($params) - 1])) array_pop($params);
    
    return $params;
    

    }

  3. Route to the proper page dynamically:

    // get the base page string, must be done after params are parsed
    public static function getCurPage() {
        global $params;
    
        // default is home
        if(empty($params))
        return self::PAGE_HOME;
        // see if it is an ajax request
        else if($params[0] == self::PAGE_AJAX)
        return self::PAGE_AJAX;
        // see if it is a multi param page, and if not, return error
        else {
            // store this, as we are going to use it in the loop condition
            $numParams = count($params);
    
            // initialize to full params array
            $testParams = $params;
            // $i = number of params to include in the current page name being checked, {1, .., n}
            for($i = $numParams; $i > 0; $i--) {
                // get test page name
                $page = strtolower(implode('/', $testParams));
    
                // if the page exists, return it
                if(self::pageExists($page))
                    return $page;
    
                // pop the last param off
                array_pop($testParams);
            }
    
            // page DNE go to error page
            return self::PAGE_ERROR;
        }
    }
    

The value here is that it looks for the most specific page to the least specific page. Also, workout outside of a framework gives you complete control so if there's a bug somewhere, you know you can fix it - you don't have to look up some weird workaround in your framework.

So now that $params is global, any page that uses a parameter simply calls $params[X] to work with it. Friendly URLs without a framework.

The way I add pages then is I put them into an array that is looked at in the pageExists($page) call.

For AJAX calls, I put in a special IF:

// if ajax, include it and finish
if($page == PageHelper::PAGE_AJAX) {
    PageHelper::includeAjaxPage();
    $db->disconnect();
    exit;
}

And voila - your own micro routing framework.


The problem is that a routing is a complex thing in a framework.

Perhaps you take a look at Silex. Its a micro-framework based on the Symfony2 Components. Its not so big as Symfony2 but have some of the features.


Need Your Help

Image direct comparison vs hashing

performance hash

I have a bunch of images to download, after their downloaded I'd like to check them against one "special" image to see if there exactly the same as this image. To do this I could just compare the a...

communication between python programs

python process

I have a python program that is running as a daemon on Linux.