1

My controller looks like:

use Psr\Http\Message\ServerRequestInterface;

class HelloController extends AbstractController
{
    public function __invoke(ServerRequestInterface $request): Response
    {
        dd($request)
        return $this->json([]);
    }

After going to URL of this controller I get the following error:

Cannot autowire argument $request of "HelloController()": it references interface "Psr\Http\Message\ServerRequestInterface" but no such service exists. Did you create a class that implements this interface?

My composer.json looks like:

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.4",
        "nyholm/psr7": "^1.1",
        "psr/http-client": "^1.0",
        "sensio/framework-extra-bundle": "^6.1",
        "symfony/asset": "^4.1",
        "symfony/flex": "^1.0",
        "symfony/framework-bundle": "^4.1",
        "symfony/messenger": "^4.1",
        "symfony/psr-http-message-bridge": "^1.0",
        "symfony/security-bundle": "^4.1",
        "symfony/yaml": "^4.1",
        "php-http/httplug": "^2.0",
        "php-http/guzzle6-adapter": "^2.0",
        "guzzlehttp/psr7": "^1.5"
    },

I have not set anything in services.yaml file.

miclofa
  • 49
  • 1
  • 10
  • Interesting. Did not realize the bridge was still around. It never did get used much. The examples I have seen used the zendframework/zend-diactoros package. Don't know if the PsrServerRequestParamConverter needs some configuration for the nyholm package. You should probably do yourself a favor and at least use Symfony 4.4. – Cerad May 23 '21 at 11:21
  • It's weird because on Symfony toolbar it shows that I have Symfony 4.4, but in `composer.json`, it says that I have 4.1 LOL Do you know why? – miclofa May 23 '21 at 11:50
  • If I had to guess I would say that you are looking at one directory but running code from another. It happens. You seem to have a very short list of dependencies. Consider creating a fresh 4.4 project, install the bridge and the nyholm packages and see if that works. – Cerad May 23 '21 at 12:03
  • ... and, if not, install the package `"psr/http-message": "^1.0",` as well and try again. – PajuranCodes May 23 '21 at 19:34

2 Answers2

2

The HttpFoundation request object to PSR7 ServerRequestInterface implementation conversion was performed by SensioFrameworkExtraBundle.

But the support was removed in v6

Either downgrade to v5.5 or switch to alternative.

The following bundle provides the removed feature and is based on the original implementation.

https://github.com/ajgarlag/psr-http-message-bundle

Joyce Babu
  • 19,602
  • 13
  • 62
  • 97
-1

This is how you'd retrieve the request stack on Symfony 4.x version.

use Symfony\Component\HttpFoundation\RequestStack;

class HelloController 
{      

  public function __construct(RequestStack $requestStack)
  {
      $request = $requestStack->getCurrentRequest();
  }

} 

The above was extracted from here:- https://symfony.com/doc/4.4/service_container/request.html

Anjana Silva
  • 8,353
  • 4
  • 51
  • 54
  • The question was about retrieving a PSR-7 based request object. Not the Symfony one. When properly configured the message bridge does just that. I might add that using the request stack to retrieve the current request in a constructor will often fail miserably. – Cerad May 24 '21 at 13:40