SlideShare a Scribd company logo
1 of 62
Download to read offline
PSR-7 and PSR-15
Why can't you ignore them?
github/@sergiors
twitter/@serg1ors
The PHP Framework
Interop Group
PHP Standard
Recommendation
HTTP Protocol
https://symfony.com/doc/current/introduction/http_fundamentals.html
HTTP messages are the
foundation of web
development
GET https://www.php-fig.org/psr/psr-7/ HTTP/1.1
Accept: text/html,application/xhtml+xml,application/
xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.5
Cache-Control: no-cache
Connection: keep-alive
Host: www.php-fig.org
Pragma: no-cache
User-Agent: Mozilla/5.0 Gecko/20100101 Firefox/59.0
https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
Every HTTP request message has a
specific form
HTTP/1.1 200 OK
Cache-Control: max-age=300
Content-Encoding: br
Content-Type: text/html
Date: Thu, 19 Apr 2018 23:32:18 GMT
Etag: W/"5acdc656-1ab41"
Expires: Wed, 11 Apr 2018 09:14:49 GMT
Last-Modified: Wed, 11 Apr 2018 08:24:54 GMT
Strict-Transport-Security: max-age=0
Vary: Accept-Encoding
X-Debug-Info: eyJyZXRyaWVzIjowfQ==
Transfer-Encoding: chunked
https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6
Common Gateway
Interface (CGI)
PHP’s Server APIs (SAPI)
https://www.php-fig.org/psr/psr-7/
PSR-7: HTTP message
interfaces
PSR-7 describes common interfaces for
representing HTTP messages as
described in RFC 7230 and
RFC 7231, and URIs for use with HTTP
messages as described in RFC 3986.
https://www.php-fig.org/psr/psr-7/
But for what?
HTTP’s abstraction
Allow you create your own
implementation
PSR-7 tells you how to
build http application
Manage memory
$ composer require psr/http-message
use PsrHttpMessageMessageInterface;
use PsrHttpMessageRequestInterface;
use PsrHttpMessageResponseInterface;
use PsrHttpMessageServerRequestInterface;
use PsrHttpMessageStreamInterface;
use PsrHttpMessageUploadedFileInterface;
use PsrHttpMessageUriInterface;
PSR-7 Interfaces
https://www.php-fig.org/psr/psr-7/
https://www.php-fig.org/psr/psr-7/
use PsrHttpMessageRequestInterface;
use PsrHttpMessageServerRequestInterface;
use PsrHttpMessageResponseInterface;
https://www.php-fig.org/psr/psr-7/
use PsrHttpMessageMessageInterface;
use PsrHttpMessageUriInterface;
use PsrHttpMessageStreamInterface;
https://www.php-fig.org/psr/psr-7/
$body = (new Stream)->write(
json_encode([
'foo' => 'bar',
]
));
use PsrHttpMessageStreamInterface;
class Image implements StreamInterface
{
/* etc. */
}
$image = new Image(__DIR__ . '/cats.png');
$filename = sprintf(
'%s.%s',
create_uuid(),
pathinfo(
$file0->getClientFilename(),
PATHINFO_EXTENSION
)
);
$file0->moveTo(DATA_DIR . '/' . $filename);
$stream = new Psr7StreamWrapper($file1->getStream());
stream_copy_to_stream($stream, $s3wrapper);
use PsrHttpMessageUploadedFileInterface;
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'files' => array(
'name' => array(
0 => 'file0.txt',
1 => 'file1.html',
),
'type' => array(
0 => 'text/plain',
1 => 'text/html',
),
/* etc. */
),
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'files' => array(
0 => array(
'name' => 'file0.txt',
'type' => 'text/plain',
/* etc. */
),
1 => array(
'name' => 'file1.html',
'type' => 'text/html',
/* etc. */
),
),
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'avatar' => array(
'tmp_name' => 'phpUxcOty',
'name' => 'my-avatar.png',
'size' => 90996,
'type' => 'image/png',
'error' => 0,
),
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'avatar' => array(
'tmp_name' => 'phpUxcOty',
'name' => 'my-avatar.png',
'size' => 90996,
'type' => 'image/png',
'error' => 0,
),
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'avatar' => /* UploadedFileInterface instance */
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'files' => array(
0 => /* UploadedFileInterface instance */,
1 => /* UploadedFileInterface instance */,
2 => /* UploadedFileInterface instance */,
),
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'files' => array(
'name' => array(
0 => 'file0.txt',
1 => 'file1.html',
),
'type' => array(
0 => 'text/plain',
1 => 'text/html',
),
/* etc. */
),
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'files' => array(
0 => array(
'name' => 'file0.txt',
'type' => 'text/plain',
/* etc. */
),
1 => array(
'name' => 'file1.html',
'type' => 'text/html',
/* etc. */
),
),
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'avatar' => array(
'tmp_name' => 'phpUxcOty',
'name' => 'my-avatar.png',
'size' => 90996,
'type' => 'image/png',
'error' => 0,
),
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'avatar' => array(
'tmp_name' => 'phpUxcOty',
'name' => 'my-avatar.png',
'size' => 90996,
'type' => 'image/png',
'error' => 0,
),
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'avatar' => /* UploadedFileInterface instance */
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
array(
'files' => array(
0 => /* UploadedFileInterface instance */,
1 => /* UploadedFileInterface instance */,
2 => /* UploadedFileInterface instance */,
),
)
https://www.php-fig.org/psr/psr-7/#16-uploaded-files
Final conclusion
Solid API for HTTP
representation
Compatible with evolution
PHP ecosystem
https://www.php-fig.org/psr/psr-15/
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 as described
by PSR-7 or subsequent replacement
PSRs.
https://www.php-fig.org/psr/psr-15/
Pipeline is a series of processing stages
$ ps aux | grep firefox | grep -v grep | awk '{print $2}'
Middleware
Delegation
$ composer require psr/http-server-middleware
use PsrHttpServerRequestHandlerInterface;
use PsrHttpServerMiddlewareInterface;
PSR-15: HTTP Server Request Handlers
https://www.php-fig.org/psr/psr-15/
use PsrHttpServerRequestHandlerInterface;
https://www.php-fig.org/psr/psr-15/#21-psrhttpserverrequesthandlerinterface
use PsrHttpServerMiddlewareInterface;
https://www.php-fig.org/psr/psr-15/#22-psrhttpservermiddlewareinterface
$app->pipe(new DoctrineOrmMiddleware(/* etc. */));
$app->get('/api/users/{id}', [
new AuthorizationMiddleware(/* etc. */),
function (ServerRequestInterface $req) {
try {
return new TextResponse(
(string) $req
->getAttribute('orm')
->getRepository(User::class)
->findOneById(
$req->getAttribute('id', false)
)
);
} catch (Throwable $e) {}
throw new NotFoundException;
},
]);
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$result = $this->router->match($request);
$request = $request
->withAttribute(RouteResult::class, $result);
if ($result->isSuccess()) {
foreach ($result->getMatchedParams() as $param => $value) {
$request = $request->withAttribute($param, $value);
}
}
return $handler->handle($request);
}
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$result = $request
->getAttribute(RouteResult::class, false);
if ($result instanceof RouteResult) {
$this->helper->setRouteResult($result);
}
return $handler->handle($request);
}
https://github.com/phly/psr7examples
https://docs.zendframework.com/zend-stratigility/
https://docs.zendframework.com/zend-diactoros/
https://github.com/middlewares
https://symfony.com/doc/current/introduction/http_fundamentals.html
PSR-7 and PSR-15 is real, these
PSRs affects how the library
for http is written in PHP
PSRs is a historical landmark
for PHP ecosystem
General purpose
“I would advise students to pay more
attention to the fundamental ideas rather
than the latest technology. The technology
will be out-of-date before they graduate.
Fundamental ideas never get out of date.”
–David Lorge Parnas
https://www.sigsoft.org/SEN/parnas.html
Thank you!
twitter.com/serg1ors

github.com/sergiors

More Related Content

What's hot

State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVMState: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
Jonas Bonér
 
Crittografia: dal cifrario di Cesare a Wikileaks
Crittografia: dal cifrario di Cesare a WikileaksCrittografia: dal cifrario di Cesare a Wikileaks
Crittografia: dal cifrario di Cesare a Wikileaks
sdonk
 

What's hot (20)

Spring boot
Spring bootSpring boot
Spring boot
 
Distributed Locking in Kubernetes
Distributed Locking in KubernetesDistributed Locking in Kubernetes
Distributed Locking in Kubernetes
 
Step-by-Step Introduction to Apache Flink
Step-by-Step Introduction to Apache Flink Step-by-Step Introduction to Apache Flink
Step-by-Step Introduction to Apache Flink
 
Reactive Java (33rd Degree)
Reactive Java (33rd Degree)Reactive Java (33rd Degree)
Reactive Java (33rd Degree)
 
Hacking Adobe Experience Manager sites
Hacking Adobe Experience Manager sitesHacking Adobe Experience Manager sites
Hacking Adobe Experience Manager sites
 
Tcache Exploitation
Tcache ExploitationTcache Exploitation
Tcache Exploitation
 
Nori: The Official Elasticsearch Plugin for Korean Language Analysis
Nori: The Official Elasticsearch Plugin for Korean Language AnalysisNori: The Official Elasticsearch Plugin for Korean Language Analysis
Nori: The Official Elasticsearch Plugin for Korean Language Analysis
 
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVMState: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
 
Java SE 9 modules (JPMS) - an introduction
Java SE 9 modules (JPMS) - an introductionJava SE 9 modules (JPMS) - an introduction
Java SE 9 modules (JPMS) - an introduction
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
ElasticSearch in action
ElasticSearch in actionElasticSearch in action
ElasticSearch in action
 
암호화 이것만 알면 된다.
암호화 이것만 알면 된다.암호화 이것만 알면 된다.
암호화 이것만 알면 된다.
 
Struts Interceptors
Struts InterceptorsStruts Interceptors
Struts Interceptors
 
Building Advanced XSS Vectors
Building Advanced XSS VectorsBuilding Advanced XSS Vectors
Building Advanced XSS Vectors
 
How Sentry can help us with bugs
How Sentry can help us with bugsHow Sentry can help us with bugs
How Sentry can help us with bugs
 
Crittografia: dal cifrario di Cesare a Wikileaks
Crittografia: dal cifrario di Cesare a WikileaksCrittografia: dal cifrario di Cesare a Wikileaks
Crittografia: dal cifrario di Cesare a Wikileaks
 
Camel Day Italia 2021 - Camel K
Camel Day Italia 2021 - Camel KCamel Day Italia 2021 - Camel K
Camel Day Italia 2021 - Camel K
 
Getting Started with SQLite
Getting Started with SQLiteGetting Started with SQLite
Getting Started with SQLite
 
Azure Key Vault Integration in Scala
Azure Key Vault Integration in ScalaAzure Key Vault Integration in Scala
Azure Key Vault Integration in Scala
 
JVM Memory Management Details
JVM Memory Management DetailsJVM Memory Management Details
JVM Memory Management Details
 

Similar to PSR-7 and PSR-15, why can't you ignore them

LAMP security practices
LAMP security practicesLAMP security practices
LAMP security practices
Amit Kejriwal
 

Similar to PSR-7 and PSR-15, why can't you ignore them (20)

Securing Your Web Server
Securing Your Web ServerSecuring Your Web Server
Securing Your Web Server
 
LAMP security practices
LAMP security practicesLAMP security practices
LAMP security practices
 
Php intro
Php introPhp intro
Php intro
 
Php intro
Php introPhp intro
Php intro
 
Php intro
Php introPhp intro
Php intro
 
PHP Hypertext Preprocessor
PHP Hypertext PreprocessorPHP Hypertext Preprocessor
PHP Hypertext Preprocessor
 
GDG DevFest 2013 - PHP Web Apps on Google Cloud
GDG DevFest 2013 - PHP Web Apps on Google CloudGDG DevFest 2013 - PHP Web Apps on Google Cloud
GDG DevFest 2013 - PHP Web Apps on Google Cloud
 
Key features PHP 5.3 - 5.6
Key features PHP 5.3 - 5.6Key features PHP 5.3 - 5.6
Key features PHP 5.3 - 5.6
 
Web application security
Web application securityWeb application security
Web application security
 
Tornadoweb
TornadowebTornadoweb
Tornadoweb
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middleware
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Php manish
Php manishPhp manish
Php manish
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 
Rapid java backend and api development for mobile devices
Rapid java backend and api development for mobile devicesRapid java backend and api development for mobile devices
Rapid java backend and api development for mobile devices
 
Php intro
Php introPhp intro
Php intro
 
NodeJs
NodeJsNodeJs
NodeJs
 
Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 

PSR-7 and PSR-15, why can't you ignore them