0

我的控制器看起来像:

use Psr\Http\Message\ServerRequestInterface;

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

转到此控制器的 URL 后,我收到以下错误:

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?

我的 composer.json 看起来像:

{
    "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"
    },

我没有在services.yaml文件中设置任何内容。

4

2 回答 2

1

HttpFoundation请求对象到 PSR7实现的ServerRequestInterface转换由SensioFrameworkExtraBundle.

但是支持被删除v6

降级v5.5或切换到替代方案。

以下捆绑包提供了已删除的功能,并且基于原始实现。

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

于 2021-09-05T09:49:33.080 回答
0

这就是您在 Symfony 4.x 版本上检索请求堆栈的方式。

use Symfony\Component\HttpFoundation\RequestStack;

class HelloController 
{      

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

} 

以上是从这里提取的:- https://symfony.com/doc/4.4/service_container/request.html

于 2021-05-24T09:23:21.790 回答