Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTTP Patterns: PSR 7 & 15

HTTP Patterns: PSR 7 & 15

Provavelmente, seu código já usa uma das PSRs HTTP de alguma forma. Veremos como as mensagens HTTP (PSR-7) e os manipuladores HTTP (PSR-15) funcionam e como podem ser aproveitados de maneira adequada.

As mensagens HTTP fornecem uma vantagem considerável sobre as variáveis globais e sua imutabilidade torna mais fácil modificar com segurança uma solicitação de entrada ou uma resposta de saída em seu aplicativo da web.

Manipuladores de HTTP são um conceito comum em microestruturas, e a PSR-15 fornece uma maneira de criar código agnóstico de estrutura reutilizável. Mas não é só isso que ele oferece. A interface do manipulador HTTP pode ser usada para injetar o conceito de middleware em um aplicativo existente sem a necessidade de introduzir uma nova estrutura. Ou para construir facilmente um aplicativo leve sem qualquer estrutura.

Vamos fazer um tour guiado por ambas as PSRs e descubra como você pode usá-los com eficácia em sua base de código atual.

Jackson F. de A. Mafra

August 19, 2021
Tweet

More Decks by Jackson F. de A. Mafra

Other Decks in Programming

Transcript

  1. JULY 11, 2021 - 11H Meeting with Company A AUGUST

    8, 2021 - 16H Meeting with Company A JUNE 15, 2021 - 18H Meeting with Company A JUNE 15, 2021 - 15H Meeting with Company A JUNE 15, 2021 - 15H Meeting with Company A Meeting with Company A MARCH 22, 2021 - 15H RIL 15, 2021 - 15H ting with Company A JUNE 15, 2021 - 18H Meeting with Company A JUNE 15, 2021 - 15H Meeting with Company A JUNE 15, 2021 - 15H Meeting with Company A HTTP Patterns: PSR 7 & 15 PSRs, HTTP, Middleware / / AUGUST 2021 JACKSON MAFRA @jacksonfdam
  2. Jackson Mafra Developer Developer for over 20 years with a

    background in e-commerce and real estate projects, since 2009 with interests focused on the development of mobile interfaces and corporate applications. Call me there... http://about.me/jacksonfdam http://linkedin.com/in/jacksonfdam @jacksonfdam
  3. JUNE 25, 2021 - 12H Meeting with Company A JULY

    11, 2021 - 11H Meeting with Company A AUGUST 8, 2021 - 16H Meeting with Company A JUNE 15, 2021 - 18H Meeting with Company A JUNE 15, 2021 - 15H Meeting with Company A JUNE 15, 2021 - 15H Meeting with Company A MARCH 22, 2021 - 15H Meeting with Company A RIL 15, 2021 - 15H ting with Company A 01 HTTP
  4. What is HTTP? The Hypertext Transfer Protocol (HTTP is the

    foundation of the World Wide Web, and is used to load web pages using hypertext links. HTTP is an application layer protocol designed to transfer information between networked devices and runs on top of other layers of the network protocol stack. A typical flow over HTTP involves a client machine making a request to a server, which then sends a response message.
  5. What’s in an HTTP request? An HTTP request is the

    way internet communications platforms such as web browsers ask for the information they need to load a website. Each HTTP request made across the Internet carries with it a series of encoded data that carries different types of information. A typical HTTP request contains:
  6. What’s in an HTTP request? Request URL https://www.google.com/ Request Method:

    GET Status Code: 200 Remote Address: [28003f040018122004443 Referrer Policy: strict-origin-when-cross-origin
  7. What’s in an HTTP request? The specific version of HTTP

    followed. HTTP and HTTP/2 are the two versions. A URL. This points to the resource on the web. An HTTP method. This indicates the specific action the request expects to receive from the server in its response. HTTP request headers. This includes data such as what type of browser is being used and what data the request is seeking from the server. It can also include cookies, which show information previously sent from the server handling the request. An HTTP body. This is optional information the server needs from the request, such as user forms -- username/password logins, short responses and file uploads -- that are being submitted to the website.
  8. What’s an HTTP method? An HTTP method, sometimes referred to

    as an HTTP verb, indicates the action that the HTTP request expects from the queried server. For example, two of the most common HTTP methods are GET and POST; a GET request expects information back in return (usually in the form of a website), while a POST request typically indicates that the client is submitting information to the web server (such as form information, e.g. a submitted username and password).
  9. What’s in an HTTP response? The HTTP response message is

    the data received by a client device from the web server. As its name suggests, the response is the server's reply to an HTTP request. The information contained in an HTTP response is tailored to the context the server received from the request.
  10. What’s in an HTTP response? HTTP responses typically include the

    following data: HTTP status code, which indicates the status of the request to the client device. Responses may indicate success, an informational response, a redirect, or errors on the server or client side. HTTP response headers, which send information about the server and requested resources. An HTTP body (optional). If a request is successful, this contains the requested data in the form of HTML code, which is translated into a web page by the client browser.
  11. What’s an HTTP status code? HTTP status codes are 3-digit

    codes most often used to indicate whether an HTTP request has been successfully completed. Status codes are broken into the following 5 blocks: • 1xx Informational • 2xx Success • 3xx Redirection • 4xx Client Error • 5xx Server Error The “xx” refers to different numbers between 00 and 99.
  12. What are HTTP response headers? Much like an HTTP request,

    an HTTP response comes with headers that convey important information such as the language and format of the data being sent in the response body.
  13. What’s in an HTTP response body? Successful HTTP responses to

    ‘GET’ requests generally have a body which contains the requested information. In most web requests, this is HTML data which a web browser will translate into a web page. Or the JSON that will be consumed by your APP, WebApp, Client.
  14. JUNE 25, 2021 - 12H Meeting with Company A JULY

    11, 2021 - 11H Meeting with Company A AUGUST 8, 2021 - 16H Meeting with Company A JUNE 15, 2021 - 18H Meeting with Company A JUNE 15, 2021 - 15H Meeting with Company A JUNE 15, 2021 - 15H Meeting with Company A MARCH 22, 2021 - 15H Meeting with Company A RIL 15, 2021 - 15H ting with Company A 02 PSRs What are PHP's PSRs?
  15. What are PHP's PSRs? PSRs are PHP Standards Recommendations. They

    are a standard and clearly defined way to do many common tasks. → https://www.php-fig.org/psr/
  16. What are PHP's PSRs? 1 Basic Coding Standard 3 Logger

    Interface 4 Autoloading Standard 6 Caching Interface 7 HTTP Message Interface 11 Container Interface 12 Extended Coding Style 13 Hypermedia Links 14 Event Dispatcher 15 HTTP Handlers 16 Simple Cache 17 HTTP Factories 18 HTTP Client Accepted
  17. PSR-7 : HTTP Message Interface PSR 7 is a set

    of interfaces for HTTP messages and URIs, used when PHP is used as a web server (i.e. over HTTP.
  18. PSR-15 - HTTP Server Request Handlers PSR15 describes common interfaces

    for HTTP server request handlers and HTTP server middleware components that use HTTP messages.
  19. Routing enables us to define a URL pattern that maps

    to the request handler. This request handler can be a file or class. / / Router and Routes
  20. Route defines the URL pattern and handler information. All the

    configured routes of an application stored in Route Table and will be used by the Routing engine to determine appropriate handler class or file for an incoming request. / / Router and Routes
  21. A middleware component is an individual component participating, often together

    with other middleware components, in the processing of an incoming request and the creation of a resulting response, as defined by PSR7. / / Middleware aka middleman
  22. A middleware component MAY create and return a response without

    delegating to a request handler, if sufficient conditions are met. / / Middleware
  23. PSR-17: HTTP Factories PSR7 did not include a recommendation on

    how to create HTTP objects, which leads to difficulties when needing to create new HTTP objects within components that are not tied to a specific implementation of PSR7.
  24. PSR-18: HTTP Client The goal of this PSR is to

    allow developers to create libraries decoupled from HTTP client implementations, making libraries more reusable as it reduces the number of dependencies and lowers the likelihood of version conflicts.
  25. PSR-8: Huggable Interface This standard establishes a common way for

    objects to express mutual appreciation and support by hugging. This allows objects to support each other in a constructive fashion, furthering cooperation between different PHP projects. PSR8 is a spoof PSR (currently in Draft) proposed by Larry Garfield as an April Fools joke on 1 April 2014. https://github.com/dave1010/php-fig-psr-8 Implement
  26. References Hypertext Transfer Protocol -- HTTP/1.1 https://datatracker.ietf.org/doc/html/rfc2616 Introduction to HTTP/2

    https://developers.google.com/web/fundamentals/performance/http2 PHP packages for HTTP https://httpsoft.org/docs/app/v1/psr-7-and-psr-15 HTTP message interfaces https://github.com/php-fig/fig-standards/blob/master/accepted/PSR7-http-message.md HTTP Server Request Handlers https://github.com/php-fig/fig-standards/blob/master/accepted/PSR15-request-handlers.md HTTP Factories https://github.com/php-fig/fig-standards/blob/master/accepted/PSR17-http-factory.md
  27. References List of HTTP status codes https://en.wikipedia.org/wiki/List_of_HTTP_status_codes From HTTP Messages

    to PSR7 What’s It All About? https://www.sitepoint.com/from-http-messages-to-psr-7-whats-it-all-about/ PSR7 Objects Are Not Immutable http://andrew.carterlunn.co.uk/programming/2016/05/22/psr-7-is-not-immutable.html PSR7 By Example https://mwop.net/blog/20150126-psr-7-by-example.html PSR15 https://mwop.net/blog/20180123-psr-15.html Lumen https://lumen.laravel.com/docs/8.x/routing
  28. References A Case for Higher Level PHP Streams in PSR7

    https://mtdowling.com/blog/2014/07/03/a-case-for-higher-level-php-streams/ Requiring cURL in Your PHP Library https://mtdowling.com/blog/2013/05/02/requiring-curl-in-your-php-library/ Laravel - HTTP Client https://laravel.com/docs/7.x/http-client The HttpFoundation Component https://symfony.com/doc/current/components/http_foundation.html Working With HTTP Requests https://codeigniter.com/user_guide/concepts/http.html# HTTP Client Extension for Yii 2 https://www.yiiframework.com/extension/yiisoft/yii2-httpclient/doc/guide/2.0/en/basic-usage
  29. References PHPHTTP standardized HTTP for PHP https://docs.php-http.org/en/latest/index.html Guzzle https://docs.guzzlephp.org/en/stable/overview.html Middleware

    https://route.thephpleague.com/5.x/middleware/ HTTP https://route.thephpleague.com/5.x/http/ Route https://route.thephpleague.com/5.x/routes/ The League of Extraordinary Packages https://thephpleague.com/
  30. Meeting with Company A JUNE 15, 2021 - 15H Meeting

    with Company A JUNE 15, 2021 - 15H Meeting with Company A MARCH 22, 2021 - 15H Meeting with Company A JUNE 25, 2021 - 12H Meeting with Company A JULY 11, 2021 - 11H Meeting with Company A AUGUST 8, 2021 - 16H JUNE 15, 2021 - 18H Meeting with Company A JUNE 15, 2021 - 15H Meeting with Company A CREDITS This presentation template was created by Slidesgo, including icons by Flaticon, infographics & images by Freepik Any questions, criticisms or suggestions just contact me: THANKS! @jacksonfdam [email protected] Please keep this slide for attribution / / Confloss 2021 - HTTP Patterns: PSR 7 & 15