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 695fe21

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

File tree

Expand file treeCollapse file tree

3 files changed

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

3 files changed

+93
-0
lines changed

‎cookbook/index.rst

Copy file name to clipboardExpand all lines: cookbook/index.rst
+1Lines changed: 1 addition & 0 deletions
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

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
+88Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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
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

0 commit comments

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