Criando uma API com Zend Expressive 3
Juciellen Cabrera PHP Developer - 4Linux | Rankdone @jucycabrera #PHPWomen
Expressive PSR-15 Middleware in Minutes 2018-03-16 >= PHP 7.1
https://docs.zendframework.com/zend-stratigility/v3/middleware/
PSR-15
Middleware is any code sitting between a request and a response;
namespace PsrHttpServer; use PsrHttpMessageResponseInterface; use PsrHttpMessageServerRequestInterface; interface MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface; }
A request handler is an individual component that processes a request and produces a response, as defined by PSR-7.
namespace PsrHttpServer; use PsrHttpMessageResponseInterface; use PsrHttpMessageServerRequestInterface; interface RequestHandlerInterface { public function handle(ServerRequestInterface $request): ResponseInterface; }
PSR-7
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. Server-side code receives an HTTP request message, and returns an HTTP response message.
Instalação composer create-project zendframework/zend-expressive-skeleton expressive
[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
Pipeline
The terminology "pipeline" is often used to describe the onion. One way of looking at the "onion" is as a queue, which is first-in-first-out (FIFO) in operation. This means that the first middleware on the queue is executed first, and this invokes the next, and so on (and hence the "next" terminology).
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); // Seed the UrlHelper with the routing results: $app->pipe(UrlHelperMiddleware::class); $app->pipe(DispatchMiddleware::class); $app->pipe(NotFoundHandler::class); }
function (ServerRequestInterface $request, ResponseInterface $response, callable $next) { $response = $next($request, $response); return $response->withHeader('X-Test', time()); }
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
Obrigada! @jucycabrera jucarol17@gmail.com

Criando uma API com Zend Expressive 3