Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dev talk2

87 views

Published on

DevTalk Zoox sobre PSR-7, Middlewares PHP e Zend Expressive

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Dev talk2

  1. 1. ZOOX DEVTALK #2 PSR-7 E MIDDLEWARES JULHO / 2016
  2. 2. PSR – O QUE SIGNIFICA? PHP Standards Recommendations Número Título 1 Basic Coding Standard 2 Coding Style Guide 3 Logger Interface 4 Autoloading Standard 6 Caching Interface 7 HTTP Message Interface
  3. 3. PSR7 – MESSAGES • Request POST /path HTTP/1.1 Host: example.com foo=bar&baz=bat • PsrHttpMessageRequestInterface • Response HTTP/1.1 200 OK Content-Type: text/plain This is the response body • PsrHttpMessageResponseInterface
  4. 4. PSR7 - HEADERS $message = $message->withHeader('foo', 'bar'); echo $message->getHeaderLine('foo'); // bar echo $message->getHeaderLine('FOO'); // bar $message = $message->withHeader('fOO', 'baz'); echo $message->getHeaderLine('foo'); // baz $message = $message ->withHeader('foo', 'bar') ->withAddedHeader('foo', 'baz'); $header = $message->getHeaderLine('foo'); // 'bar, baz' $header = $message->getHeader('foo'); // ['bar', 'baz']
  5. 5. PHP MIDDLEWARES • Um middleware recebe uma Request, uma Response e faz algo com elas. • Todo middleware DEVE retornar uma PsrHttpMessageResponseInterface • Todo middleware pode chamar o próximo da fila
  6. 6. MIDDLEWARE - EXEMPLO <?php /** * Exemplo de um middleware * * @param PsrHttpMessageServerRequestInterface $request PSR7 request * @param PsrHttpMessageResponseInterface $response PSR7 response * @param callable $next Próximo * * @return PsrHttpMessageResponseInterface */ function ($request, $response, $next) { $response->getBody()->write('BEFORE'); $response = $next($request, $response); $response->getBody()->write('AFTER'); return $response; };
  7. 7. FLUXO COM MIDDLEWARE $app = new MiddlewareRunner(); $app->add('/contact', new ContactFormMiddleware()); $app->add('/forum', new ForumMiddleware()); $app->add('/blog', new BlogMiddleware()); $app->add('/store', new EcommerceMiddleware()); $app->run($request, $response); $app = new MiddlewareRunner(); $app->add('/zf2', new Zf2Middleware()); $app->add('/symfony', new SymfonyMiddleware()); $app->add('/cake', new CakeMiddleware()); $app->add('/laravel', new LaravelMiddleware()); $app->run($request, $response);
  8. 8. APLICAÇÃO COM MIDDLEWARE use ZendStratigilityMiddlewarePipe; use ZendDiactorosServer; require __DIR__ . '/../vendor/autoload.php'; $app = new MiddlewarePipe(); $server = Server::createServer($app, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); $app->pipe('/', function ($req, $res, $next) { if (! in_array($req->getUri()->getPath(), ['/', ''], true)) { return $next($req, $res); } return $res->end('Hello world!'); }); $app->pipe('/foo', function ($req, $res, $next) { return $res->end('FOO!'); }); $server->listen();
  9. 9. ZEND EXPRESSIVE • Microframework voltada para PSR-7 middlewares • Recursos: • Routing • Container (container-interopt) • Templating • Error Handling • Pipelines
  10. 10. EXPRESSIVE - EXEMPLO <?php use ZendExpressiveAppFactory; chdir(dirname(__DIR__)); require 'vendor/autoload.php'; $app = AppFactory::create(); $app->get('/', function ($request, $response, $next) { $response->getBody()->write('Hello, world!'); return $response; }); $app->pipeRoutingMiddleware(); $app->pipeDispatchMiddleware(); $app->run();

×