monii/nikic-fast-route-psr7-middleware

This package is abandoned and no longer maintained. No replacement package was suggested.

PSR-7 Routing Middleware using Nikita Popov's fast request router (nikic/fast-route)

dev-master / 0.0.x-dev 2015-11-16 12:07 UTC

This package is not auto-updated.

Last update: 2016-12-03 15:06:44 UTC


README

PSR-7 routing middleware built on top of nikic/fast-route.

Latest Stable Version Total Downloads Latest Unstable Version License
Build Status

Requirements

  • PHP 5.5+

Installation

$> composer require monii/nikic-fast-route-psr7-middleware

Until a stable version has been released or if a development version is preferred, use:

$> composer require monii/nikic-fast-route-psr7-middleware:@dev

Usage

Instantiation

Instantiating NikicFastRoute requires providing an instance of FastRoute\Dispatcher. An optional action attribute name and parameters attribute name may be specified as well. The default values for the action attribute name and parameters attribute name are found in NikicFastRoute::DEFAULT_ACTION_ATTRIBUTE_NAME ("monii/nikic-fast-route:action") and NikicFastRoute::DEFAULT_PARAMETERS_ATTRIBUTE_NAME ("monii/nikic-fast-route:parameters").

Middleware that execute after NikicFastRoute will be able to examine these request attributes to determine which action and parameters should be used to dispatch the request.

Invocation

When invoked, NikicFastRoute will examine a PSR-7 ServerRequestInterface and pass relevant information from the request to the FastRoute\Dispatcher instance to dispatch the request.

Dispatcher error conditions Dispatcher::NOT_FOUND and Dispatcher::METHOD_NOT_ALLOWED will result in returning the provided PSR-7 ResponseInterface returned with status code 404 and 405 respectively.

Dispatcher condition Dispatcher::FOUND will result in the the dispatched action being added to the configured action attribute and the dispatched parameters being added to the configured parameters attribute on the incoming request. The response value of $next (called with the modified $request and original $response) will be returned.

If for some reason any other condition comes back from Dispatcher (it is unclear if this is even possible), then the provided $response will be returned with the status code of 500.

Consuming

Consumers of NikicFastRoute managed requests can examine the request's attributes to determine how to handle the request. The responsibility for handling the dispatched action can fall on a framework, an application, or another middleware.

An example of middleware that consumes NikicFastRoute is monii/action-handler-psr7-middleware. While this middleware is configured by default to look at a different attribute name for its action, it can be easily configured to use the same name as this middleware. Conversely, monii/nikic-fast-route-psr7-middleware itself can be configured to use the same names as monii/action-handler-psr7-middleware.

In the case of a framework, both middleware can be configured to use an entirely different set of names. One framework that does this is Nimble. Nimble intentionally configures both of these middleware to use its own action attribute name.

Example

An example of using this middleware in conjuction with others can be seen in looking at now Nimble operates. Nimble leverages Relay for its middleware dispatcher and Laravel's container for dependency injection and service location.

$container->bind(Relay::class, function (Container $container) {
    /** @var RelayBuilder $relayBuilder */
    $relayBuilder = $container->make(RelayBuilder::class);

    $queue = array_merge(
        [
            $container->make(NikicFastRoute::class, [
                'actionAttributeName' => WebApp::ACTION_ATTRIBUTE_NAME,
                'parametersAttributeName' => WebApp::PARAMETERS_ATTRIBUTE_NAME,
            ]),
        ],
        $container->tagged('middleware.error_handler'),
        $container->tagged('middleware.early'),
        $container->tagged('middleware'),
        $container->tagged('middleware.late'),
        [
            $container->make(ActionHandler::class, [
                'actionAttributeName' => WebApp::ACTION_ATTRIBUTE_NAME,
            ]),
        ]
    );

    return $relayBuilder->newInstance($queue);
});

As shown in this example, Nimble configures both NikicFastRoute and ActionHandler to use the same attribute name for the action, WebApp::DEFAULT_ACTION_ATTRIBUTE_NAME. This ensures that both middleware are able to communicate effectively on the topic of the action that should be dispatched for a request.

License

MIT, see LICENSE.

Community

Want to get involved? Here are a few ways:

  • Find us in the #monii IRC channel on irc.freenode.org.
  • Mention @moniidev on Twitter.