Routing vs Piping

Expressive provides two mechanisms for adding middleware to your application:

Piping

zend-stratigility provides a mechanism termed piping for composing middleware in an application. When you pipe middleware to the application, it is added to a queue, and dequeued in order until a middleware returns a response instance.

Expressive adds the ability to segregate middleware to a specific path; as an example:

$app->pipe('/api', $apiMiddleware);

will execute $apiMiddleware only if the path matches /api; otherwise, it will skip over that middleware.

This path segregation, however, is limited: it will only match literal paths. This is done purposefully, to provide excellent baseline performance, and to prevent feature creep in the library.

Path segregation

Internally, when Application::pipe() detects two arguments, it calls Zend\Stratigility\path() using the two arguments in order to create a Zend\Stratigility\Middleware\PathMiddlewareDecorator instance; this latter is what performs the actual path checking.

Expressive uses and exposes piping to users, with one addition: middleware may be specified by service name or an array of service names, and zend-expressive will lazy-load the service only when the middleware is invoked.

In order to accomplish the lazy-loading, zend-expressive wraps the calls to fetch and dispatch the middleware inside a Zend\Expressive\Middleware\LazyLoadingMiddleware instance; as such, there is no overhead to utilizing service-based middleware until it is dispatched.

Routing

Routing is the process of discovering values from the incoming request based on defined criteria. That criteria might look like:

In each of the above, if the router determines that the request matches the criteria, it will indicate:

Most routers allow you to define arbitrarily complex rules, and many even allow you to define:

As such, routing is more powerful than the literal path matching used when piping, but it is also more costly (though routers such as FastRoute largely make such performance issues moot).

When to Pipe

In Expressive, we recommend that you pipe middleware in the following circumstances:

When to Route

Use routing when:

The above cover most use cases; in other words, most middleware should be added to the application as routed middleware.

Controlling middleware execution order

As noted in the earlier section on piping, piped middleware is queued, meaning it has a FIFO ("first in, first out") execution order.

Additionally, zend-expressive's routing and dispatch capabilities are themselves implemented as piped middleware.

To ensure your middleware is piped correctly, keep in mind the following:

As an example:

$app->pipe(OriginalMessages::class);
$app->pipe(ServerUrlMiddleware::class);
$app->pipe(XClacksOverhead::class);
$app->pipe(ErrorHandler::class);
$app->pipe(RouteMiddleware::class);
$app->pipe(ImplicitHeadMiddleware::class);
$app->pipe(ImplicitOptionsMiddleware::class);
$app->pipe(MethodNotAllowedMiddleware::class);
$app->pipe(UrlHelperMiddleware::class);
$app->pipe(AuthorizationCheck::class);
$app->pipe(DispatchMiddleware::class);
$app->pipe(NotFoundHandler::class);