The Middleware Express

Matthew Weier O'Phinney

Software bug
PHPUnit
Heartbleed

Version 1 Frameworks

CakePHP Symfony Zend Framework

Version 2+ Frameworks

CakePHP Symfony Zend Framework Laravel

Standards

Composer
PHP-FIG
Interfaces
Icons made by Smashicons from www.flaticon.com is licensed by CC 3.0 BY

PSR-7

PSR-15

HTTP Request
HTTP Response
PHP
Python Ruby

Requests / Responses

Middleware as Queue
Middleware as Onion
Layered Middleware
From the CakePHP 3.0 manual
PHP

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;
}
          
Node.js

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);
}
          
Layered Middleware
From the CakePHP 3.0 manual
Teams

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

Zend Framework Laravel

PHP

PSR-7

PSR-15

Middleware commoditizes how web applications are built

and who can build them.

PHP

Matthew Weier O'Phinney

PHP Developer

@mwop