dev-master
9999999-devA simple router based on nikic/fastroute, providing a higher-level interface and more features
MIT
The Requires
- php >=5.6.0
- nikic/fast-route 0.6.*
- php-di/invoker ~1.2
The Development Requires
by Austin Hyde
A simple router based on nikic/fastroute, providing a higher-level interface and more features
Building on top of nikic/fastroute, and inspired by the routing portion of the Macaron web framework, this library is a simple, extensible, non-opinionated, and fast routing library for PHP web applications and services., (*2)
Important: This is still alpha-quality, but I want to make it real. If you try it out and find any problems, please open an issue!, (*3)
composer require austinhyde/patchboard
They say an example is worth a thousand words. Or that might be something else., (*4)
<?php $req = Request::createFromGlobals(); $authorized = function() use ($req) { list($user, $pass) = explode(':', base64_decode( explode(' ', $req->headers->get('Authorization'))[1] )); if ($user != 'admin' && $pass != 'admin') { return new JSONResponse(['message'=>'Not Authorized'], 403); } }; $json = function($c) use ($req) { $r = $c->nextHandler(); if ($r !== null && !($r instanceof Response)) { return new JSONResponse($r, 200); } }; $r = new Patchboard\Router; $r->group('/users', function($r) { $r->get('/', function() { return [['user_id' => 1, 'username' => 'foo']]; }); $r->get('/{id}', function(Context $ctx) { return ['user_id' => $ctx->getPathParam('id'), 'username' => 'foo']; }); }, $authorized, $json); try { $response = $r->dispatch($req->getMethod(), $req->getPathInfo()); } catch (Patchboard\RouteNotFoundException $ex) { $response = new JSONResponse(['message' => $ex->getMessage()], 404); } catch (Patchboard\MethodNotAllowedException $ex) { $response = new JSONResponse(['message' => $ex->getMessage()], 405, ['Allow' => $ex->getAllowed()]); } $response->send();
Put into words:, (*5)
/users
prefix. Note the trailing , $authorized, $json
, which instructs the group to apply the $authorized
and $json
handler./users
, and instruct the router to call the corresponding controller methods.The driving principle behind Patchboard is to be as simple as possible, while being flexible enough to use in any application. Patchboard does this
by only being a route dispatcher. That's it. At it's core, it's a glorified call_user_func
implementation, that pattern matches the function name.
The only assumption it makes is that you are routing on an HTTP method and request path., (*6)
The core unit of Patchboard is the route. Each route matches on an HTTP method and path, and has any number of handlers attached to it., (*7)
A handler is simply either a callable, or an implementation of the Patchboard\HandlerInterface
interface. The callable (or handle
method) receives the route context
as an argument. The handler then can do things like authenticate the user, process the HTTP request, transform outputs, etc., (*8)
The route context is an object containing information on the matched route and assigned handlers. Handlers can use the hasNextHandler()
and handleNext()
methods
to see if there's another handler to call, and to invoke the next handler, respectively. handleNext()
returns the result of the next handler in the sequence. This
allows you to "wrap" inner handlers and manipulate their results., (*9)
For any list of handlers assigned to a route, handlers will be executed sequentially until a non-null result is returned, or there are no more handlers. Handlers that invoke the next handler themselves advance the internal cursor to the next, so that no handlers are called twice., (*10)
A simple router based on nikic/fastroute, providing a higher-level interface and more features
MIT