Skip to content

Commit 23bf4a4

Browse files
committed
[PSR-7] Bridge documentation
1 parent 8b0c026 commit 23bf4a4

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

cookbook/map.rst.inc

Lines 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

Lines 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy