Quick Overview
The symfony/psr-http-message-bridge is a PHP library that provides a bridge between Symfony's HttpFoundation component and PSR-7 HTTP message interfaces. It allows developers to convert Symfony Request and Response objects to PSR-7 compliant objects and vice versa, enabling better interoperability between Symfony and PSR-7 based applications.
Pros
- Seamless integration between Symfony and PSR-7 compliant libraries
- Improves code portability and reusability across different PHP frameworks
- Maintains compatibility with both Symfony's HttpFoundation and PSR-7 standards
- Lightweight and easy to integrate into existing projects
Cons
- Adds an additional layer of abstraction, which may impact performance slightly
- Requires understanding of both Symfony's HttpFoundation and PSR-7 interfaces
- May introduce complexity in projects that don't require PSR-7 compatibility
Code Examples
- Converting a Symfony Request to a PSR-7 ServerRequest:
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Component\HttpFoundation\Request;
use Nyholm\Psr7\Factory\Psr17Factory;
$psr17Factory = new Psr17Factory();
$psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
$symfonyRequest = Request::createFromGlobals();
$psrRequest = $psrHttpFactory->createRequest($symfonyRequest);
- Converting a PSR-7 Response to a Symfony Response:
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Nyholm\Psr7\Response;
$psr7Response = new Response(200, ['Content-Type' => 'text/html'], '<html><body>Hello, World!</body></html>');
$httpFoundationFactory = new HttpFoundationFactory();
$symfonyResponse = $httpFoundationFactory->createResponse($psr7Response);
- Using the bridge in a Symfony controller:
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Component\HttpFoundation\Response;
class MyController
{
public function index(ServerRequestInterface $request)
{
// Work with PSR-7 request
$name = $request->getQueryParams()['name'] ?? 'World';
// Create PSR-7 response
$psr7Response = new \Nyholm\Psr7\Response(200, [], "Hello, $name!");
// Convert PSR-7 response to Symfony response
$httpFoundationFactory = new HttpFoundationFactory();
return $httpFoundationFactory->createResponse($psr7Response);
}
}
Getting Started
- Install the library using Composer:
composer require symfony/psr-http-message-bridge nyholm/psr7
- Use the bridge in your Symfony application:
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Nyholm\Psr7\Factory\Psr17Factory;
$psr17Factory = new Psr17Factory();
$psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
$httpFoundationFactory = new HttpFoundationFactory();
// Now you can use $psrHttpFactory to convert Symfony requests to PSR-7
// and $httpFoundationFactory to convert PSR-7 responses to Symfony responses
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PSR-7 Bridge
Provides integration for PSR7.
Resources
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot