+-----------+------+--------------+-------+
| subject | its | mean | diff |
+-----------+------+--------------+-------+
| Sunrise | 1000 | 17,856.609μs | 1.00x |
| FastRoute | 1000 | 20,920.968μs | 1.17x |
| Aura | 1000 | 44,480.588μs | 2.49x |
| Zend | 1000 | 96,778.725μs | 5.42x |
+-----------+------+--------------+-------+
composer require sunrise/http-router
Study sunrise/awesome-skeleton to understand how this can be used.
This package has annotation support, all the details are here.
The example uses other sunrise packages, but you can use for example
zend/diactoros
, or any other.
composer require sunrise/http-message sunrise/http-server-request
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Sunrise\Http\Message\ResponseFactory;
use Sunrise\Http\Router\Exception\MethodNotAllowedException;
use Sunrise\Http\Router\Exception\RouteNotFoundException;
use Sunrise\Http\Router\RouteCollection;
use Sunrise\Http\Router\Router;
use Sunrise\Http\ServerRequest\ServerRequestFactory;
class DemoMiddleware implements MiddlewareInterface
{
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler) : ResponseInterface
{
$response = $handler->handle($request);
$response->getBody()->write(sprintf('URI: "%s"; Attributes: "%s"',
$request->getUri(), print_r($request->getAttributes(), true)
));
return $response;
}
}
$routes = new RouteCollection();
$routes->get('home', '/')
->addMiddleware(new DemoMiddleware);
$routes->group('/api', function($routes)
{
$routes->group('/v1', function($routes)
{
$routes->post('resource.create', '/resource')
->addMiddleware(new DemoMiddleware);
$routes->patch('resource.update', '/resource/{id}')
->addPattern('id', '\d+')
->addMiddleware(new DemoMiddleware);
$routes->delete('resource.delete', '/resource/{id}')
->addPattern('id', '\d+')
->addMiddleware(new DemoMiddleware);
$routes->get('resource.read', '/resource/{id}')
->addPattern('id', '\d+')
->addMiddleware(new DemoMiddleware);
$routes->get('resource.all', '/resource')
->addMiddleware(new DemoMiddleware);
});
});
$router = new Router();
$router->addRoutes($routes);
try
{
$response = $router->handle(ServerRequestFactory::fromGlobals());
}
catch (MethodNotAllowedException $e)
{
$response = (new ResponseFactory)->createResponse(405)
->withHeader('allow', implode(',', $e->getAllowedMethods()));
$response->getBody()->write($response->getReasonPhrase());
}
catch (RouteNotFoundException $e)
{
$response = (new ResponseFactory)->createResponse(404);
$response->getBody()->write($response->getReasonPhrase());
}
$headers = $response->getHeaders();
foreach ($headers as $name => $values)
{
foreach ($values as $value)
{
header(sprintf('%s: %s', $name, $value), false);
}
}
header(sprintf('HTTP/%s %d %s',
$response->getProtocolVersion(),
$response->getStatusCode(),
$response->getReasonPhrase()
), true);
echo $response->getBody();
$route = $routes->route('route.id', '/route/path', ['HEAD', 'GET']);
$route = $routes->head('route.id', '/route/path');
$route = $routes->get('route.id', '/route/path');
$route = $routes->post('route.id', '/route/path');
$route = $routes->put('route.id', '/route/path');
$route = $routes->patch('route.id', '/route/path');
$route = $routes->delete('route.id', '/route/path');
$route = $routes->purge('route.id', '/route/path');
$route = $routes->safe('route.id', '/route/path');
$route = $routes->any('route.id', '/route/path');
// Add a route to the collection with the path: /foo/bar/baz/qux
$routes->group('/foo', function($routes)
{
$routes->group('/bar', function($routes)
{
$routes->group('/baz', function($routes)
{
$route = $routes->get('qux', '/qux');
});
});
});
$route = $routes->any('resource.action', '/resource/{action}(/{id})')
->addPattern('action', 'create|update|delete|read|all')
->addPattern('id', '\d+');
$route
->addMiddleware(new FooMiddleware)
->addMiddleware(new BarMiddleware)
->addMiddleware(new BazMiddleware);
$router
->addMiddleware(new FooMiddleware)
->addMiddleware(new BarMiddleware)
->addMiddleware(new BazMiddleware);
$route = $router->match($request);
Error handling (whoops)
composer require middlewares/whoops
$router->addMiddleware(new \Middlewares\Whoops());
Payload (payload)
composer require middlewares/payload
$router->addMiddleware(new \Middlewares\JsonPayload());
$router->addMiddleware(new \Middlewares\UrlEncodePayload());
Encoding (encoder)
composer require middlewares/encoder
$router->addMiddleware(new \Middlewares\GzipEncoder());
https://github.com/middlewares
php vendor/bin/phpunit