I enjoy simplicity, in everything. Unnecessary complexity is evil. Have a look at my WordPress plugins.

PHP Routing Class which supports REST routing

In the last few months I’ve been looking into all the different PHP frameworks and how they glue things together. One of the most important things in every framework is the way it handles routes. I’ve been developing Ruby on Rails applications extensively last year and I really like the way Rails handles it’s routes. That’s why I’ve decided to start programming a Routing class that mimics Rails routing functionality.

I’ve released this PHP Routing class on Github here, so you can follow and contribute to it’s development progress.

What does this PHP Routing class do?

In short, this class helps you route URL’s to their corresponding Controller and action methods. You can use different HTTP methods to fully benefit from using REST / resourceful routes. Also, setting up dynamic URL’s that pass parameters to their corresponding action are no problem.

  • REST Routing
  • Use different HTTP methods for same URL’s
  • Dynamic URL segments
  • Reversed routing, create URL from route

The class is fairly simple and lightweight, it won’t do any unnecessary processing. Have a look at the following snippet taken from the example file to get a better understanding on how to use this class in your projects.

$r = new Router();

// maps / to controller "users" and method "index".
$r->match("/","users#index");

// maps /user/5 to controller "users", method "show" with parameter "id" => 5
$r->match("/user/:id","users#show");

// maps POST request to /users/ to controller "users" and method "create"
$r->match("/users","users#create",array("via" => "post"));

// maps GET /users/5/edit to controller "users", method "edit" with parameters "id" => 5 and saves route as a named route.
$r->match("/users/:id/edit","users#edit",array("via" => "get", "as" => "user_edit_page"));

// echoes /users/5/edit
echo $r->reverse("user_edit_page",array("id" => "5"));

// maps multiple routes
// GET /users will map to users#index
// GET /users/5 will map to users#show
$r->resources("users",array("only" => "index,show"));

if($r->hasRoute()) {
    // extract controller, action and parameters.
    extract($r->getRoute());
?>   
<h1>Route found!</h1>
<pre>
    Controller: 	<?php echo $controller; ?>
    Action: 		<?php echo $action; ?>
    Params: 		<?php var_dump($params); ?>
</pre><?php

} else {
    ?><h1>No route found.</h1><?php
}

If you’re using this class and experience any issues, please post them using GitHub’s issue tracker. Also, feel free to geek around and fork the project to improve this class.

Watch PHP Routing Class on Github!

UPDATE (31/07/2012): If you like PHP Router you might also like AltoRouter.

Share this post: on Twitter on Facebook

4 Responses to “PHP Routing Class which supports REST routing”

  1. Hello I liked much this very cool idea, but how i can organize my folders would be something like the folders on the rails

    You can help me?

  2. This is indeed cool and I really like the pattern.

    If you’re looking for a framework with exactly this functionality and lots of other great things, then try visiting http://yiiframework.com
    I use it for all my projects.
    The Yii Framework has also implemented the ActiveRecord pattern similarly to RoR.

  3. The library looks very cool, but for someone new to it, this is very confusing. Is the controller assumed as a PHP file? For instance, in $r->match(‘/user/:id’,’users#show’), would I have a users.php controller file? Or you expect a Users class? Apologies if the question is not constructive, but I don’t understand how to make this work exactly. Thanks for any help.

    • Hey Hamman,

      Have a look at he GitHub example as this changed in a later version of PHP-Router. Actually this class leaves that up to you. You can specify closures, arrays or plain strings as the route’s targets. You can decide what to do with the target yourself, in your own code.

      I am a bit late with this reply so I hope you figured that out by now. :-)

Leave a Reply