txs/router

Just another router

0.3.2 2014-06-07 13:30 UTC

README

Description

A simple router that matches to defined controller/classes and sends along parameters.

Example

    <?php
    use Enpere\Router\Router;
    require 'vendor/autoload.php';
    
    
    $route = new Router();
    
    $route->get('/users', 'UsersController@index');
    $route->get('/users/{id}', 'UsersController@show');
    $route->post('/users', 'UsersController@create');
    $route->put('/users', 'UsersController@update');
    $route->delete('/users/{id}', 'UsersController@destroy');
    
    echo $route->run();

Route to Controller or Closure

You can point your routes either to a Controller and a method or a closure.

To controller

     $route->get('/users', 'UsersController@index');

To Closure

     $route->get('/users/{id}', function($id) {
        echo $id;
     });

Slugs

There are three predefined slugs that you can use.

  * {id} matches digits.
  * {name} matches characters a-z, A-Z.
  * {slug} This will eat anything.

You can also define your own regex

  * $route->get('/users/([\d{1,3}])', 'UsersController@show');

You can access the parameters in the called class

    class UsersController {
      
      
      public function show($id, $name) {
        echo "$id with a $name";
      }
    
    }

Exception

Whenever a route is not found, we will throw a NotFoundException

List routes

You need to pass true (defaults to false) to the constructor of Router. new Router(true)

After that you can access the list by appending ?routes

    /users/12?routes

that will display all your registered routes.

Known issues

  • Query string is matched agains the routes. Fixed in v0.1a