My PHP Router Class

Recently I had some spare time and decided to rewrite the PHP Router I’ve been using on many of my recent projects. I prefer to have my projects setup with .htaccess which redirects all page requests to index.php which uses my router to decide which function to call. This approach allows me to write very clean, modular code.

My new router is very flexible, supports unit testing and can be extended. I’ve attached the source code to this post as well as an example of how to use it.

My new router requires PHP 5.3.0 for namespaces.

I took a lot of inspiration from the very popular framework called CodeIgniter among other projects I’ve worked on over the years.

Click here to download the source

Click here to go to the GitHub repository

 

<?php
$router = new \Igniter\Router;

// Adding a basic route
$router->route( '/login', 'login_function' );

// Adding a route with a named alphanumeric capture, using the  syntax
$router->route( '/user/view/<:username>', 'view_username' );

// Adding a route with a named numeric capture, using the  syntax
$router->route( '/user/view/<#user_id>', array( 'UserClass', 'view_user' ) );

// Adding a route with a wildcard capture (Including directory separtors), using
// the  syntax
$router->route( '/browse/<*categories>', 'category_browse' );

// Adding a wildcard capture (Excludes directory separators), using the
//  syntax
$router->route( '/browse/<!category>', 'browse_category' );

// Adding a custom regex capture using the  syntax
$router->route( '/lookup/zipcode/<:zipcode|[0-9]{5}>', 'zipcode_func' );

// Specifying priorities
$router->route( '/users/all', 'view_users', 1 ); // Executes first
$router->route( '/users/<:status>', 'view_users_by_status', 100 ); // Executes after

// Specifying a default callback function if no other route is matched
$router->default_route( 'page_404' );

// Run the router
$router->execute();

Now the most important function here is $router->route(). This function is used for adding a routing rule. The first parameter is the rule itself, the second parameter is the name of a function to execute if the rule is matched to the current URL, and the third parameter is optional and represents the priority.

9 Replies to “My PHP Router Class”

  1. Thanks Brandon for the simple and useful router class. I only need the basic thing for my project and this seems to be a good fit.

  2. Hi Brandon,

    Just wanted to say thank you for posting this code. It will definitely save me some time and I appreciate it. I’m sure I will learn from it. I’ve been playing around with piecing a small framework together.A lot of Googling and a lot of code reading. It’s been interesting and fun. Thank you again.

    Best Regards,

    Bill

  3. Very Nice 🙂 I’m actually using this.

    Although the only way I could avoid an exception for the default route was to change the code to this:


    if (!$matched_route && $this->default_route !== null) {
    // Store the parameters and callback function to execute later
    $this->params = $this->url_clean;
    $this->callback = $this->default_route;
    return array('params' => $this->url_clean, 'callback' => $this->default_route, 'route' => false, 'original_route' => false);
    }

    I also changed all Exceptions to \Exception.

  4. Looks great! too bad that the source link is dead.
    Anyone can help me with that?

    1. I’ve fixed the link

  5. Wow
    awesome
    simple and powerfull

  6. Every line a comment?? Reallyyyyyy????????

    1. Comments in no way affect the performance of PHP, but they are very useful to novice programmers or people just starting out with PHP

  7. Brandon,

    Can I fork your Router class, adapt it and use it in the PHP framework that I’m developing?

    The framework is open source, BSD and MIT license and can be found at http://www.github.com/ramirovjr

    At the framework code / documentation / wiki I’ll make a reference to your website.

Leave a Reply