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