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 5b79a55

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

File tree

Expand file treeCollapse file tree

4 files changed

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

4 files changed

+109
-0
lines changed

‎bridges/index.rst

Copy file name to clipboard
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The Bridges
2+
==============
3+
4+
.. toctree::
5+
:hidden:
6+
7+
psr7
8+
9+
.. include:: /components/map.rst.inc

‎bridges/map.rst.inc

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* **PSR-7**
2+
3+
* :doc:`/bridges/psr7`

‎bridges/psr7.rst

Copy file name to clipboard
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
.. index::
2+
single: PSR-7
3+
single: Bridges; PSR-7
4+
5+
The PSR-7 Bridge
6+
================
7+
8+
The PSR-7 bridge converts :doc:`HttpFoundation </components/http_foundation/index>`
9+
objects from and to objects implementing HTTP message interfaces defined
10+
by the `PSR-7`_.
11+
12+
Installation
13+
------------
14+
15+
You can install the component in 2 different ways:
16+
17+
* :doc:`Install it via Composer </components/using_components>` (``symfony/psr-http-message-bridge`` on `Packagist`_);
18+
* Use the official Git repository (https://github.com/symfony/psr-http-message-bridge).
19+
20+
The bridge also needs a PSR-7 implementation to allow converting HttpFoundation
21+
objects to PSR-7 objects. It provides native support for _`Zend Diactoros`_.
22+
Use Composer (``zendframework/zend-diactoros`` on `Packagist`_) or refers to
23+
the project documentation to install it.
24+
25+
Usage
26+
-----
27+
28+
Converting from HttpFoundation objects to PSR-7
29+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30+
31+
The bridge provides an interface of a factory called :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+
$psrRequest = $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 :class:`Symfony\\Bridge\\PsrHttpMessage\\HttpFoundationFactoryInterface`
64+
that builds HttpFoundation objects from objects implementing PSR-7 interfaces.
65+
66+
The next snippet explain how to convert an object implementing the :class:`Psr\\Http\\Message\\ServerRequestInterface`
67+
interface to a :class:`Symfony\\Component\\HttpFoundation\\Request` instance::
68+
69+
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
70+
71+
// $psrRequest is an instance of Psr\Http\Message\ServerRequestInterface
72+
73+
$httpFoundationFactory = new HttpFoundationFactory();
74+
$symfonyRequest = $httpFoundationFactory->createRequest($psrRequest);
75+
76+
From an object implementing the :class:`Psr\\Http\\Message\\ResponseInterface`
77+
to a :class:`Symfony\\Component\\HttpFoundation\\Response` instance::
78+
79+
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
80+
81+
// $psrResponse is an instance of Psr\Http\Message\ResponseInterface
82+
83+
$httpFoundationFactory = new HttpFoundationFactory();
84+
$symfonyResponse = $httpFoundationFactory->createResponse($psrResponse);
85+
86+
.. _`PSR-7`: http://www.php-fig.org/psr/psr-7/
87+
.. _Packagist: https://packagist.org/packages/symfony/psr-http-message-bridge

‎index.rst

Copy file name to clipboardExpand all lines: index.rst
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ Components
6565

6666
Read the :doc:`Components </components/index>` documentation.
6767

68+
Bridges
69+
-------
70+
71+
.. toctree::
72+
:hidden:
73+
74+
bridges/index
75+
76+
Read the :doc:`Bridges </bridges/index>` documentation.
77+
6878
Reference Documents
6979
-------------------
7080

0 commit comments

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