2017 © Pedro Peláez
 

library fast-route

Middleware to use FastRoute

image

middlewares/fast-route

Middleware to use FastRoute

  • Monday, July 23, 2018
  • by oscarotero
  • Repository
  • 4 Watchers
  • 35 Stars
  • 17,922 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 12 Versions
  • 23 % Grown

The README.md

middlewares/fast-route

Latest Version on Packagist ![Software License][ico-license] Testing ![Total Downloads][ico-downloads], (*1)

Middleware to use FastRoute for handler discovery., (*2)

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/fast-route., (*3)

composer require middlewares/fast-route

You may also want to install middlewares/request-handler., (*4)

Example

This example uses middlewares/request-handler to execute the route handler:, (*5)

//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)
        echo sprintf('Hello %s', $name);

        //Or return a string
        return sprintf('Hello %s', $name);

        //Or return a response
        return new Response();
    });
});

$dispatcher = new Dispatcher([
    new Middlewares\FastRoute($dispatcher),
    new Middlewares\RequestHandler()
]);

$response = $dispatcher->dispatch(new ServerRequest('/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., (*6)

Usage

Create the middleware with a FastRoute\Dispatcher instance:, (*7)

$route = new Middlewares\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., (*8)

$responseFactory = new MyOwnResponseFactory();

$route = new Middlewares\FastRoute($dispatcher, $responseFactory);

attribute

Changes the attribute name used to store the handler in the server request. The default name is request-handler., (*9)

$dispatcher = new Dispatcher([
    //Save the route handler in an attribute called "route"
    (new Middlewares\FastRoute($dispatcher))->attribute('route'),

    //Execute the route handler
    (new Middlewares\RequestHandler())->handlerAttribute('route')
]);

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details., (*10)

The MIT License (MIT). Please see LICENSE for more information., (*11)

The Versions

23/07 2018
27/01 2018
19/04 2017

dev-feature/discovery-invoke

dev-feature/discovery-invoke https://github.com/middlewares/fast-route

Middleware to use FastRoute

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware psr-7 server http router psr-15 fastroute fast-route

26/12 2016
27/11 2016