The Middleware Express
Matthew Weier O'Phinney
Version 1 Frameworks
Version 2+ Frameworks
Standards
PSR-7
PSR-15
Requests / Responses
From the CakePHP 3.0 manual
PSR-7
function (
ServerRequestInterface $request
) : ResponseInterface
class Middleware
{
private $app;
public function __construct($app)
{
$this->app = $app;
}
}
class Middleware
{
private $app;
public function setApp($app)
{
$this->app = $app;
}
}
class Middleware
{
public $app;
}
function (req, res, next)
next(req, res)
app.use(logging);
app.use(caching);
app.use(error_handling);
app.use(routing);
http.createServer(app).listen(80);
PSR-15
interface RequestHandler
{
public function handle(
Request $request
) : Response;
}
interface Middleware
{
public function process(
Request $request,
RequestHandler $handler
) : Response;
}
$response = $handler->handle($request);
Standards
Remove the need for frameworks
$app->pipe(CorsMiddleware::class);
$app->pipe(SessionMiddleware::class);
$app->pipe(AuthenticationMiddleware::class);
$app->pipe(AuthorizationMiddleware::class);
$app->pipe(ValidationMiddleware::class);
$app->patch('/api/books/{id:\d+}', [
CorsMiddleware::class,
SessionMiddleware::class,
AuthenticationMiddleware::class,
AuthorizationMiddleware::class,
ValidationMiddleware::class,
UpdateBookHandler::class,
]);
public function process(Request $request, RequestHandler $handler)
{
$id = $request->getAttribute('id', false);
if (false === $id) {
throw new MissingBookIdentifierException();
}
$user = $request->getAttribute('user');
$book = $this->repository->fetch($id, $user);
return new JsonResponse($book);
}
From the CakePHP 3.0 manual
Each developer...
- Needs to know PHP.
- Needs to know the shared libraries of the organization.
- Needs to know the framework and tooling used by the project.
- Needs to know the domain language for the project.
Hiring Developers
PHP
PSR-7
PSR-15
Middleware commoditizes how web applications are built
and who can build them.
Matthew Weier O'Phinney
PHP Developer
@mwop