Introducing Traffic

Traffic is a routing library for PHP 5.3 and above.

I created traffic to take the pain out of mapping incoming requests to your site to code that does whatever you need it to do. Most existing solutions, like the atrocious Zend_Controller_Router_Rewrite whatever assume that you want to map requests to method on a class, with enough magic mixed in to be a pain in the arse.

With Traffic, I’ve taken the approach that you should be in charge of how you organise your code. All Traffic does is the matching, and leaves the rest up to you. if you’re a framework monkey with an incessant need for hand holding, then move on, coz I’m not gonna tell you how to structure your app or anything like that.

Traffic is inspired by Sinatra & Sammy JS without pretending that it’s not PHP.

Here’s a snippet:

use Fu\Traffic as t;

// relative_to is an alias to rel, in case you prefer to be more verbose
t::relative_to('/user/:username', function () {
  t::get(function ($params) {
    echo 'Viewing user:'.$params['username'];
  });

  t::post(function ($params) {
    echo 'Creating user:'.$params['username'];
  });

  t::put(function ($params) {
    echo 'Updating user:'.$params['username'];
  });

  t::delete(function ($params) {
   echo 'Deleting user:'.$params['username'];
  });
});

Go check out the code and a smidgen of documentation.