Authenticating with middleware
One very important usage of middleware is to provide authentication. Most web-based applications need the ability to verify a visitor via username and password. By incorporating PSR-7 standards into an authentication class, you will make it generically useful across the board, so to speak, being secure enough that it can be used in any framework that provides PSR-7-compliant request and response objects.
How to do it...
We begin by defining an
Application\Acl\AuthenticateInterface
class. We use this interface to support the Adapter software design pattern, making ourAuthenticate
class more generically useful by allowing a variety of adapters, each of which can draw authentication from a different source (for example, from a file, using OAuth2, and so on). Note the use of the PHP 7 ability to define the return value data type:namespace Application\Acl; use Psr\Http\Message\ { RequestInterface, ResponseInterface }; interface AuthenticateInterface { public function...