Zend Expressive 3 PSR-15
PSR-15 Middleware in Minutes
Juciellen
Cabrera
PHP Developer - 4Linux | Rankdone
@jucycabrera
#PHPWomen
Expressive
PSR-15 Middleware in Minutes
2018-03-16
>= PHP 7.1
Instalação
composer create-project zendframework/zend-expressive-skeleton expressive
requires
● php: ^7.1
● fig/http-message-util: ^1.1.2
● psr/container: ^1.0
● psr/http-message: ^1.0.1
● psr/http-server-middleware: ^1.0
● zendframework/zend-expressive-router: ^3.0
● zendframework/zend-expressive-template: ^2.0
● zendframework/zend-httphandlerrunner: ^1.0.1
● zendframework/zend-stratigility: ^3.0
[1] Minimal (no default middleware, templates, or
assets; configuration only)
[2] Flat (flat source code structure; default selection)
[3] Modular (modular source code structure;
recommended)
Rotas
● Aura.Router
● Fast Router
● Zend Route
return function (Application $app, MiddlewareFactory $factory, ContainerInterface
$container) : void {
$app->get('/', AppActionHomePageAction::class, 'home');
$app->post('/album', AppActionAlbumCreateAction::class, 'album.create');
$app->put('/album/:id', AppActionAlbumUpdateAction::class, 'album.put');
$app->patch('/album/:id', AppActionAlbumUpdateAction::class, 'album.patch');
$app->delete('/album/:id', AppActionAlbumDeleteAction::class, 'album.delete');
$app->route('/contact', AppActionContactAction::class, ['GET', 'POST', ...], 'contact');
$app->any('/contact', AppActionContactAction::class)->setName('contact');
$app->route(
'/contact',
AppActionContactAction::class,
ZendExpressiveRouterRoute::HTTP_METHOD_ANY,
'contact'
);
Injeção de Dependência
[1] Aura.Di
[2] Pimple
[3] zend-servicemanager
[4] Auryn
[5] Symfony DI Container
Templates
[1] Plates
[2] Twig
[3] zend-view installs zend-servicemanager
[n] None of the above
Manipulação de erro
[1] Whoops
[n] None of the above
PSR-7
HTTP message interfaces
Interfaces de mensagens HTTP
HTTP messages are the foundation of web
development. Web browsers and HTTP
clients such as cURL create HTTP request messages
that are sent to a web server,
which provides an HTTP response message.
Mensagens HTTP são a base do desenvolvimento
web. Browsers e clients HTTP como cURL criam
mensagens de requisição HTTP que são enviadas
para um servidor web, que provisiona uma
mensagem de resposta HTTP.
Server-side code receives an HTTP
request message, and returns an HTTP response
message.
O código do lado do servidor recebe uma
mensagem de requisição HTTP e retorna uma
mensagem de resposta HTTP.
Interfaces
● MessageInterface.php
● RequestInterface.php
● ResponseInterface.php
● ServerRequestInterface.php
● StreamInterface.php
● UploadedFileInterface.php
● UriInterface.php
<?php
namespace PsrHttpMessage;
interface MessageInterface
{
public function getProtocolVersion();
public function withProtocolVersion($version);
public function getHeaders();
public function hasHeader($name);
public function getHeader($name);
public function getHeaderLine($name);
public function withHeader($name, $value);
public function withAddedHeader($name, $value);
public function withoutHeader($name);
public function getBody();
public function withBody(StreamInterface $body);
}
PSR-15
A request handler is an individual component that
processes a request and
produces a response, as defined by PSR-7.
Um request handler (manipulador de requisição é
um componente individual que processa uma
requisição e produz uma resposta, como definido
pela PSR-7.
namespace PsrHttpServer;
use PsrHttpMessageResponseInterface;
use PsrHttpMessageServerRequestInterface;
interface RequestHandlerInterface
{
public function handle(ServerRequestInterface $request):
ResponseInterface;
}
namespace AppHandler;
use PsrHttpMessageResponseInterface;
use PsrHttpMessageServerRequestInterface;
use PsrHttpServerRequestHandlerInterface;
use ZendDiactorosResponseJsonResponse;
class PingHandler implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request) : ResponseInterface
{
return new JsonResponse(['ack' => time()]);
}
}
Middleware is any code sitting between a request
and a response.
Middleware é qualquer código entre a requisição e a
resposta.
Middlewares
● Authentication
● Authorization
● CORS
● Log
● Routing
namespace PsrHttpServer;
use PsrHttpMessageResponseInterface;
use PsrHttpMessageServerRequestInterface;
interface MiddlewareInterface
{
public function process(ServerRequestInterface $request,
RequestHandlerInterface $handler): ResponseInterface;
}
Pipeline
The terminology "pipeline" is often used to describe the
onion. One way of looking at the "onion" is as a queue
(FIFO). This means that the first middleware on the
queue is executed first, and this invokes the next.
A terminologia “pipeline” é frequentemente usada para
descrever uma cebola. Uma forma de olhar para a
cebola é como uma fila (FIFO). Isso significa que o
primeiro middleware da fila é executado primeiro e
invoca o próximo.
return function (Application $app, MiddlewareFactory $factory, ContainerInterface
$container) : void {
$app->pipe(ErrorHandler::class);
$app->pipe(ServerUrlMiddleware::class);
$app->pipe(RouteMiddleware::class);
$app->pipe(ImplicitHeadMiddleware::class);
$app->pipe(ImplicitOptionsMiddleware::class);
$app->pipe(MethodNotAllowedMiddleware::class);
$app->pipe(UrlHelperMiddleware::class);
$app->pipe(DispatchMiddleware::class);
$app->pipe(NotFoundHandler::class);
}
Command Line Tooling
composer expressive
handler:create
middleware:create
module:create
module:register
composer run --timeout=0 serve
composer expressive handler:create “AppHandlerPhpCommunitiesCreateHandler”
PhpCommunitiesCreateHandler
PhpCommunitiesCreateHandlerFactory
Referências
https://www.php-fig.org/psr/psr-7/
https://www.php-fig.org/psr/psr-15/
https://docs.zendframework.com/zend-expressive/v3/getting-started/quick-start/
https://framework.zend.com/blog/2018-03-16-expressive-3.html
https://packagist.org/packages/zendframework/zend-expressive
Obrigada!
@jucycabrera
jucarol17@gmail.com

Zend Expressive 3 e PSR-15