borobudur / routing
Borobudur Routing Component
Installs: 563
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:borobudur-component
Requires
- php: >=5.4.0
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2017-10-23 11:55:44 UTC
README
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
- Get Composer
- Install Borobudur\Routing with
composer require borobudur/routing
- 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'));