borobudur/routing

This package is abandoned and no longer maintained. No replacement package was suggested.

Borobudur Routing Component

dev-master / 0.1.x-dev 2015-09-21 07:00 UTC

This package is not auto-updated.

Last update: 2017-10-23 11:55:44 UTC


README

Build Status License Code Climate Test Coverage Scrutinizer Code Quality SensioLabsInsight

Borobudur\Routing is high performance and lightweight Routing Component for PHP 5.4+

  • Can be used with all HTTP Methods
  • Validate accept specific HTTP Method
  • Named route parameters
  • Default value route parameter
  • Parameter requirement with regex
  • Nested route collection with prefix
  • Route self handling or design your own Router handler

Installation

  1. Get Composer
  2. Install Borobudur\Routing with composer require borobudur/routing
  3. Add composer autoload on your main PHP file: require __DIR__.'/vendor/autoload.php';

Example

Example 1 - Listen $_SERVER['SCRIPT_NAME'] for php virtual server

<?php
require_once __DIR__.'/vendor/autoload.php';

use Borobudur\Routing\RouteCollection;
use Borobudur\Routing\Route;
use Borobudur\Routing\Router;
use Borobudur\Routing\RequestContext;
use Borobudur\Routing\RouteFactory;

// Add collection
$collection = new RouteCollection();
$collection->add('default', RouteFactory::createHttpGet('/', function() {
    return 'hello world!!';
}));
$collection->add('detail', new Route(
    '/detail/{id}',
    array('id' => 1),
    array('id' => '\d+'),
    array(Route::HTTP_METHOD_GET),
    function(RequestContext $request) {
        return sprintf('Currently view detail with id %s', $request->getParameter('id'));
    }
));

$router = new Router($collection);
// Router closure handler
$router->setClosureHandler(function(Route $route = null, RequestContext $request) {
    if (null === $route) {
        echo '404 Not Found';
    } else {
        $caller = Closure::bind($route->getHandler(), null);
        echo $caller($request);
    }
});
// Listen request (only work for php virtual server) use $_SERVER['PATH_INFO'] for cgi
$router->listen(new RequestContext($_SERVER['SCRIPT_NAME'], $_SERVER['REQUEST_METHOD']));

Example 2 - Create dummy request for cli

$collection = new RouteCollection();
$collection->add('dummy', RouteFactory::createHttpGet('/dummy', function() {
    // Route self handling without route handler
    var_dump('dummy request from php cli!!');
}));

$router = new Router($collection);
$router->listen(RequestContextFactory::createHttpGet('/dummy'));

License

MIT Licensed