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

[PSR-7] Bridge documentation #5331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 cookbook/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The Cookbook
serializer
service_container/index
session/index
psr7
symfony1
templating/index
testing/index
Expand Down
4 changes: 4 additions & 0 deletions 4 cookbook/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@
* (configuration) :doc:`/cookbook/configuration/pdo_session_storage`
* :doc:`/cookbook/session/avoid_session_start`

* **PSR-7**

* :doc:`/cookbook/psr7`

* **symfony1**

* :doc:`/cookbook/symfony1`
Expand Down
88 changes: 88 additions & 0 deletions 88 cookbook/psr7.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
.. index::
single: PSR-7

The PSR-7 Bridge
================

The PSR-7 bridge converts :doc:`HttpFoundation </components/http_foundation/index>`
objects from and to objects implementing HTTP message interfaces defined
by the `PSR-7`_.

Installation
------------

You can install the component in 2 different ways:

* :doc:`Install it via Composer </components/using_components>` (``symfony/psr-http-message-bridge`` on `Packagist`_);
* Use the official Git repository (https://github.com/symfony/psr-http-message-bridge).

The bridge also needs a PSR-7 implementation to allow converting HttpFoundation
objects to PSR-7 objects. It provides native support for _`Zend Diactoros`_.
Use Composer (``zendframework/zend-diactoros`` on `Packagist`_) or refers to
the project documentation to install it.

Usage
-----

Converting from HttpFoundation Objects to PSR-7
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The bridge provides an interface of a factory called
:class:`Symfony\\Bridge\\PsrHttpMessage\\HttpMessageFactoryInterface`
that builds objects implementing PSR-7 interfaces from HttpFoundation objects.
It also provide a default implementation using Zend Diactoros internally.

The following code snippet explain how to convert a :class:`Symfony\\Component\\HttpFoundation\\Request`
to a Zend Diactoros :class:`Zend\\Diactoros\\ServerRequest` implementing the
:class:`Psr\\Http\\Message\\ServerRequestInterface` interface::

use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Symfony\Component\HttpFoundation\Request;

$symfonyRequest = new Request(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'dunglas.fr'), 'Content');
// The HTTP_HOST server key must be set to avoid an unexpected error

$psr7Factory = new DiactorosFactory();
$psrRequest = $psr7Factory->createRequest($symfonyRequest);

And now from a :class:`Symfony\\Component\\HttpFoundation\\Response` to a Zend
Diactoros :class:`Zend\\Diactoros\\Response` implementing the :class:`Psr\\Http\\Message\\ResponseInterface`
interface::

use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Symfony\Component\HttpFoundation\Response;

$symfonyResponse = new Response('Content');

$psr7Factory = new DiactorosFactory();
$psrResponse = $psr7Factory->createResponse($symfonyResponse);

Converting Objects implementing PSR-7 Interfaces to HttpFoundation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

On the other hand, the bridge provide a factory interface called
:class:`Symfony\\Bridge\\PsrHttpMessage\\HttpFoundationFactoryInterface`
that builds HttpFoundation objects from objects implementing PSR-7 interfaces.

The next snippet explain how to convert an object implementing the :class:`Psr\\Http\\Message\\ServerRequestInterface`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

interface to a :class:`Symfony\\Component\\HttpFoundation\\Request` instance::

use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;

// $psrRequest is an instance of Psr\Http\Message\ServerRequestInterface

$httpFoundationFactory = new HttpFoundationFactory();
$symfonyRequest = $httpFoundationFactory->createRequest($psrRequest);

From an object implementing the :class:`Psr\\Http\\Message\\ResponseInterface`
to a :class:`Symfony\\Component\\HttpFoundation\\Response` instance::

use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;

// $psrResponse is an instance of Psr\Http\Message\ResponseInterface

$httpFoundationFactory = new HttpFoundationFactory();
$symfonyResponse = $httpFoundationFactory->createResponse($psrResponse);

.. _`PSR-7`: http://www.php-fig.org/psr/psr-7/
.. _Packagist: https://packagist.org/packages/symfony/psr-http-message-bridge
Morty Proxy This is a proxified and sanitized view of the page, visit original site.