Skip to content

Commit 8f5338d

Browse files
committed
minor #5512 [Cookbook] Backport PSR-7 bridge docs to 2.3 (dunglas, weaverryan)
This PR was merged into the 2.3 branch. Discussion ---------- [Cookbook] Backport PSR-7 bridge docs to 2.3 | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | all | Fixed tickets | I think we made a mistake in merging #5331 into the `2.7` branch as the bridge is also [compatible with the 2.3 version of the HttpFoundation component](https://github.com/symfony/psr-http-message-bridge/blob/master/composer.json#L21). Commits ------- fe9759a [PSR-7] Fix Diactoros link c6f10c9 [#5331] Tiny typo 6df293c [PSR-7] Bridge documentation
2 parents c7ebd2c + fe9759a commit 8f5338d

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

cookbook/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The Cookbook
2727
serializer
2828
service_container/index
2929
session/index
30+
psr7
3031
symfony1
3132
templating/index
3233
testing/index

cookbook/map.rst.inc

+4
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@
193193
* (configuration) :doc:`/cookbook/configuration/mongodb_session_storage`
194194
* :doc:`/cookbook/session/avoid_session_start`
195195

196+
* **PSR-7**
197+
198+
* :doc:`/cookbook/psr7`
199+
196200
* **symfony1**
197201

198202
* :doc:`/cookbook/symfony1`

cookbook/psr7.rst

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
.. index::
2+
single: PSR-7
3+
4+
The PSR-7 Bridge
5+
================
6+
7+
The PSR-7 bridge converts :doc:`HttpFoundation </components/http_foundation/index>`
8+
objects from and to objects implementing HTTP message interfaces defined
9+
by the `PSR-7`_.
10+
11+
Installation
12+
------------
13+
14+
You can install the component in 2 different ways:
15+
16+
* :doc:`Install it via Composer </components/using_components>` (``symfony/psr-http-message-bridge`` on `Packagist`_);
17+
* Use the official Git repository (https://github.com/symfony/psr-http-message-bridge).
18+
19+
The bridge also needs a PSR-7 implementation to allow converting HttpFoundation
20+
objects to PSR-7 objects. It provides native support for `Zend Diactoros`_.
21+
Use Composer (``zendframework/zend-diactoros`` on `Packagist`_) or refer to
22+
the project documentation to install it.
23+
24+
Usage
25+
-----
26+
27+
Converting from HttpFoundation Objects to PSR-7
28+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29+
30+
The bridge provides an interface of a factory called
31+
:class:`Symfony\\Bridge\\PsrHttpMessage\\HttpMessageFactoryInterface`
32+
that builds objects implementing PSR-7 interfaces from HttpFoundation objects.
33+
It also provide a default implementation using Zend Diactoros internally.
34+
35+
The following code snippet explain how to convert a :class:`Symfony\\Component\\HttpFoundation\\Request`
36+
to a Zend Diactoros :class:`Zend\\Diactoros\\ServerRequest` implementing the
37+
:class:`Psr\\Http\\Message\\ServerRequestInterface` interface::
38+
39+
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
40+
use Symfony\Component\HttpFoundation\Request;
41+
42+
$symfonyRequest = new Request(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'dunglas.fr'), 'Content');
43+
// The HTTP_HOST server key must be set to avoid an unexpected error
44+
45+
$psr7Factory = new DiactorosFactory();
46+
$psrRequest = $psr7Factory->createRequest($symfonyRequest);
47+
48+
And now from a :class:`Symfony\\Component\\HttpFoundation\\Response` to a Zend
49+
Diactoros :class:`Zend\\Diactoros\\Response` implementing the :class:`Psr\\Http\\Message\\ResponseInterface`
50+
interface::
51+
52+
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
53+
use Symfony\Component\HttpFoundation\Response;
54+
55+
$symfonyResponse = new Response('Content');
56+
57+
$psr7Factory = new DiactorosFactory();
58+
$psrResponse = $psr7Factory->createResponse($symfonyResponse);
59+
60+
Converting Objects implementing PSR-7 Interfaces to HttpFoundation
61+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62+
63+
On the other hand, the bridge provide a factory interface called
64+
:class:`Symfony\\Bridge\\PsrHttpMessage\\HttpFoundationFactoryInterface`
65+
that builds HttpFoundation objects from objects implementing PSR-7 interfaces.
66+
67+
The next snippet explain how to convert an object implementing the :class:`Psr\\Http\\Message\\ServerRequestInterface`
68+
interface to a :class:`Symfony\\Component\\HttpFoundation\\Request` instance::
69+
70+
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
71+
72+
// $psrRequest is an instance of Psr\Http\Message\ServerRequestInterface
73+
74+
$httpFoundationFactory = new HttpFoundationFactory();
75+
$symfonyRequest = $httpFoundationFactory->createRequest($psrRequest);
76+
77+
From an object implementing the :class:`Psr\\Http\\Message\\ResponseInterface`
78+
to a :class:`Symfony\\Component\\HttpFoundation\\Response` instance::
79+
80+
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
81+
82+
// $psrResponse is an instance of Psr\Http\Message\ResponseInterface
83+
84+
$httpFoundationFactory = new HttpFoundationFactory();
85+
$symfonyResponse = $httpFoundationFactory->createResponse($psrResponse);
86+
87+
.. _`PSR-7`: http://www.php-fig.org/psr/psr-7/
88+
.. _Packagist: https://packagist.org/packages/symfony/psr-http-message-bridge
89+
.. _`Zend Diactoros`: https://github.com/zendframework/zend-diactoros

0 commit comments

Comments
 (0)