PHP code example of loophp / unaltered-psr-http-message-bridge-bundle

1. Go to this page and download the library: Download loophp/unaltered-psr-http-message-bridge-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

loophp / unaltered-psr-http-message-bridge-bundle example snippets




declare(strict_types=1);

namespace App\Controller;

use Psr\Http\Message\RequestInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;

final class MainController {
    /**
     * @Route("/api/offers", name="api_offers")
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function __invoke(
        Request $request,
        HttpMessageFactoryInterface $httpMessageFactory,
        RequestInterface $psrRequest
    ): Response {
        // Using Symfony's request object.
        $uri = $request->getUri(); // http://localhost:8000/api/offers?product_color=red
        $params = $request->query->all(); // [ 'product_color' => 'red' ]

        // Using PSR Request.
        $psrRequest = $httpMessageFactory->createRequest($request);
        $uri = (string) $psrRequest->getUri(); // http://localhost:8000/api/offers?product.color=red
        $params = $psrRequest->getUri()->getQuery(); // 'product.color=red'

        // Or directly by requesting the PSR request through RequestInterface parameter.

        return new Response('');
    }
}