You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//Create the router dispatcher$dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector$r) {
$r->addRoute('GET', '/hello/{name}', function ($request) {
//The route parameters are stored as attributes$name = $request->getAttribute('name');
//You can echo the output (it will be captured and written into the body)echosprintf('Hello %s', $name);
//Or return a stringreturnsprintf('Hello %s', $name);
//Or return a responsereturnnewResponse();
});
});
$dispatcher = newDispatcher([
newMiddlewares\FastRoute($dispatcher),
newMiddlewares\RequestHandler()
]);
$response = $dispatcher->dispatch(newServerRequest('/hello/world'));
FastRoute allows anything to be defined as the router handler (a closure, callback, action object, controller class, etc). The middleware will store this handler in a request attribute.
Usage
Create the middleware with a FastRoute\Dispatcher instance:
$route = newMiddlewares\FastRoute($dispatcher);
Optionally, you can provide a Psr\Http\Message\ResponseFactoryInterface as the second argument, that will be used to create the error responses (404 or 405). If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.
Changes the attribute name used to store the handler in the server request. The default name is request-handler.
$dispatcher = newDispatcher([
//Save the route handler in an attribute called "route"
(newMiddlewares\FastRoute($dispatcher))->attribute('route'),
//Execute the route handler
(newMiddlewares\RequestHandler())->handlerAttribute('route')
]);
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.