-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[PSR-7] Bridge documentation #5331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
.. index:: | ||
single: PSR-7 | ||
|
||
The PSR-7 Bridge | ||
================ | ||
|
||
The PSR-7 bridge converts :doc:`HttpFoundation </components/http_foundation/index>` | ||
objects from and to objects implementing HTTP message interfaces defined | ||
by the `PSR-7`_. | ||
|
||
Installation | ||
------------ | ||
|
||
You can install the component in 2 different ways: | ||
|
||
* :doc:`Install it via Composer </components/using_components>` (``symfony/psr-http-message-bridge`` on `Packagist`_); | ||
* Use the official Git repository (https://github.com/symfony/psr-http-message-bridge). | ||
|
||
The bridge also needs a PSR-7 implementation to allow converting HttpFoundation | ||
objects to PSR-7 objects. It provides native support for _`Zend Diactoros`_. | ||
Use Composer (``zendframework/zend-diactoros`` on `Packagist`_) or refers to | ||
the project documentation to install it. | ||
|
||
Usage | ||
----- | ||
|
||
Converting from HttpFoundation Objects to PSR-7 | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The bridge provides an interface of a factory called | ||
:class:`Symfony\\Bridge\\PsrHttpMessage\\HttpMessageFactoryInterface` | ||
that builds objects implementing PSR-7 interfaces from HttpFoundation objects. | ||
It also provide a default implementation using Zend Diactoros internally. | ||
|
||
The following code snippet explain how to convert a :class:`Symfony\\Component\\HttpFoundation\\Request` | ||
to a Zend Diactoros :class:`Zend\\Diactoros\\ServerRequest` implementing the | ||
:class:`Psr\\Http\\Message\\ServerRequestInterface` interface:: | ||
|
||
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
$symfonyRequest = new Request(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'dunglas.fr'), 'Content'); | ||
// The HTTP_HOST server key must be set to avoid an unexpected error | ||
|
||
$psr7Factory = new DiactorosFactory(); | ||
$psrRequest = $psr7Factory->createRequest($symfonyRequest); | ||
|
||
And now from a :class:`Symfony\\Component\\HttpFoundation\\Response` to a Zend | ||
Diactoros :class:`Zend\\Diactoros\\Response` implementing the :class:`Psr\\Http\\Message\\ResponseInterface` | ||
interface:: | ||
|
||
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
$symfonyResponse = new Response('Content'); | ||
|
||
$psr7Factory = new DiactorosFactory(); | ||
$psrResponse = $psr7Factory->createResponse($symfonyResponse); | ||
|
||
Converting Objects implementing PSR-7 Interfaces to HttpFoundation | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
On the other hand, the bridge provide a factory interface called | ||
:class:`Symfony\\Bridge\\PsrHttpMessage\\HttpFoundationFactoryInterface` | ||
that builds HttpFoundation objects from objects implementing PSR-7 interfaces. | ||
|
||
The next snippet explain how to convert an object implementing the :class:`Psr\\Http\\Message\\ServerRequestInterface` | ||
interface to a :class:`Symfony\\Component\\HttpFoundation\\Request` instance:: | ||
|
||
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; | ||
|
||
// $psrRequest is an instance of Psr\Http\Message\ServerRequestInterface | ||
|
||
$httpFoundationFactory = new HttpFoundationFactory(); | ||
$symfonyRequest = $httpFoundationFactory->createRequest($psrRequest); | ||
|
||
From an object implementing the :class:`Psr\\Http\\Message\\ResponseInterface` | ||
to a :class:`Symfony\\Component\\HttpFoundation\\Response` instance:: | ||
|
||
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; | ||
|
||
// $psrResponse is an instance of Psr\Http\Message\ResponseInterface | ||
|
||
$httpFoundationFactory = new HttpFoundationFactory(); | ||
$symfonyResponse = $httpFoundationFactory->createResponse($psrResponse); | ||
|
||
.. _`PSR-7`: http://www.php-fig.org/psr/psr-7/ | ||
.. _Packagist: https://packagist.org/packages/symfony/psr-http-message-bridge |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here