Skip to content

Instantly share code, notes, and snippets.

@robertbasic
Created March 9, 2017 15:26
Show Gist options
  • Save robertbasic/aefbefec74c156731f5234ab2eaeb828 to your computer and use it in GitHub Desktop.
Save robertbasic/aefbefec74c156731f5234ab2eaeb828 to your computer and use it in GitHub Desktop.
Zend Expressive, what's up mate?
<?php
// This is all going to be in one file here, but is split to different classes in real world
// Also intentionally left out bits and pieces not required for the sake of the example
$app = new Application($router, $container);
$app->pipe(MyMiddleware::class);
$app->pipeRoutingMiddleware();
$app->pipeDispatchMiddleware();
$app->get('/', HomePage::class);
$app->run();
// Here the middleware is quite alright like this
class HomePage
{
public function __invoke($request, $delegate)
{
// do stuff, create template, return new response
return new HtmlResponse($template);
}
}
// If this MyMiddleware doesn't implement Interop\Http\ServerMiddleware\MiddlewareInterface
// then it must look like this:
// Because HomePage returns a response and MyMiddleware doesn't implement the MiddlewareInterface
class MyMiddleware
{
public function __invoke($request, $response, $delegate)
{
// do stuff
}
}
// And suddenly it **must** follow the stratigility 1.x-like __invoke signature
// But if no middleware down the chain returns a response, MyMiddleware can be like
// public function __invoke($request, $delegate)
// The docs say the new middleware https://zendframework.github.io/zend-expressive/reference/migration/to-v2/#http-interop
// can either have the process($request, $delegate) signature, or, and I quote:
// "Callable middleware that follows the above signature (the typehint for the request argument is optional)"
// where "above signature" refers to the process($request, $delegate) signature
// What does this tell us?
// That the docs lie, and that if a middleware doesn't implement the MiddlewareInterface
// the middlewares higher up the stack (piped first) will be at the mercy of the middlewares lower at the stack (piped last).
// Am I at fault? Maybe.
// But the docs say that my middleware "CAN" implement the MiddlewareInterface, which means they are not required to do so.
// And if they don't, the __invoke signature of those middlewares changes based on is there a response or not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment