Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 23bf4a4

Browse filesBrowse files
committed
[PSR-7] Bridge documentation
1 parent 8b0c026 commit 23bf4a4
Copy full SHA for 23bf4a4

File tree

Expand file treeCollapse file tree

2 files changed

+90
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+90
-0
lines changed

‎cookbook/map.rst.inc

Copy file name to clipboardExpand all lines: cookbook/map.rst.inc
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@
195195
* (configuration) :doc:`/cookbook/configuration/pdo_session_storage`
196196
* :doc:`/cookbook/session/avoid_session_start`
197197

198+
* **PSR-7** *
199+
200+
* :doc:`/cookbook/psr7`
201+
198202
* **symfony1**
199203

200204
* :doc:`/cookbook/symfony1`

‎cookbook/psr7.rst

Copy file name to clipboard
+86Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 refers 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 :class:`Symfony\\Bridge\\PsrHttpMessage\\HttpMessageFactoryInterface`
31+
that builds objects implementing PSR-7 interfaces from HttpFoundation objects.
32+
It also provide a default implementation using Zend Diactoros internally.
33+
34+
The following code snippet explain how to convert a :class:`Symfony\\Component\\HttpFoundation\\Request`
35+
to a Zend Diactoros :class:`Zend\\Diactoros\\ServerRequest` implementing the
36+
:class:`Psr\\Http\\Message\\ServerRequestInterface` interface::
37+
38+
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
39+
use Symfony\Component\HttpFoundation\Request;
40+
41+
$symfonyRequest = new Request(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'dunglas.fr'), 'Content');
42+
// The HTTP_HOST server key must be set to avoid an unexpected error
43+
44+
$psr7Factory = new DiactorosFactory();
45+
$psrRequest = $psr7Factory->createRequest($symfonyRequest);
46+
47+
And now from a :class:`Symfony\\Component\\HttpFoundation\\Response` to a Zend
48+
Diactoros :class:`Zend\\Diactoros\\Response` implementing the :class:`Psr\Http\Message\ResponseInterface`
49+
interface::
50+
51+
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
52+
use Symfony\Component\HttpFoundation\Response;
53+
54+
$symfonyResponse = new Response('Content');
55+
56+
$psr7Factory = new DiactorosFactory();
57+
$psrResponse = $psr7Factory->createResponse($symfonyResponse);
58+
59+
Converting objects implementing PSR-7 interfaces to HttpFoundation
60+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61+
62+
On the other hand, the bridge provide a factory interface called :class:`Symfony\\Bridge\\PsrHttpMessage\\HttpFoundationFactoryInterface`
63+
that builds HttpFoundation objects from objects implementing PSR-7 interfaces.
64+
65+
The next snippet explain how to convert an object implementing the :class:`Psr\\Http\\Message\\ServerRequestInterface`
66+
interface to a :class:`Symfony\\Component\\HttpFoundation\\Request` instance::
67+
68+
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
69+
70+
// $psrRequest is an instance of Psr\Http\Message\ServerRequestInterface
71+
72+
$httpFoundationFactory = new HttpFoundationFactory();
73+
$symfonyRequest = $httpFoundationFactory->createRequest($psrRequest);
74+
75+
From an object implementing the :class:`Psr\\Http\\Message\\ResponseInterface`
76+
to a :class:`Symfony\\Component\\HttpFoundation\\Response` instance::
77+
78+
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
79+
80+
// $psrResponse is an instance of Psr\Http\Message\ResponseInterface
81+
82+
$httpFoundationFactory = new HttpFoundationFactory();
83+
$symfonyResponse = $httpFoundationFactory->createResponse($psrResponse);
84+
85+
.. _`PSR-7`: http://www.php-fig.org/psr/psr-7/
86+
.. _Packagist: https://packagist.org/packages/symfony/psr-http-message-bridge

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.