Symfony 5.1 - 无法自动装配服务

Symfony 5.1 - Cannot autowire service

我想重构我的代码,但现在我有错误而且不明白是什么。 Objectif : 调用TokenService时不需要传递参数,使用自动装配自动装配EntityManager & Request,controller调用服务时不设置。

Cannot resolve argument $tokenService of App\Controller\TokenController::showTokens() Cannot autowire service App\Service\TokenService argument $request of method __construct() references class Symfony\Component\HttpFoundation\Request but no such service exists.

之前:

/src/Controller/TokenController.php

<?php

namespace App\Controller;

use App\Service\TokenService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * @Route("/v1")
 */
class TokenController
{
   /** @var EntityManagerInterface $em */
    private $em;
    /** @var Request $request */
    private $request;

    /**
     * TokenService constructor.
     *
     * @param Request $request
     * @param EntityManagerInterface $em
     */
    public function __construct(Request $request, EntityManagerInterface $em)
    {
        $this->request = $request;
        $this->em = $em;
    }

    public function showTokens(Request $request, EntityManagerInterface $em): JsonResponse
    {
        $tokenService = new TokenService($request, $em);

        return $tokenService->getTokens();
    }
}

/src/Service/TokenService.php

<?php

namespace App\Service;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class TokenService
 * @package App\Service
 */
class TokenService
{
    /** @var EntityManagerInterface $em */
    private $em;
    /** @var Request $request */
    private $request;

    /**
     * TokenService constructor.
     *
     * @param Request $request
     * @param EntityManagerInterface $em
     */
    public function __construct(Request $request, EntityManagerInterface $em)
    {
        $this->request = $request;
        $this->em = $em;
    }

    public function getTokens()
    {
        return true;
    }
}

之后:

/config/services.yaml

parameters:

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'

    App\Controller\:
        resource: '../src/Controller/'
        tags: ['controller.service_arguments']

    App\Service\TokenService: ~

/src/Controller/TokenController.php

<?php

namespace App\Controller;

use App\Service\TokenService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * @Route("/v1")
 */
class TokenController
{
    public function showTokens(Request $request, EntityManagerInterface $em, TokenService $tokenService): JsonResponse
    {
        return $tokenService->getTokens();
    }

/src/Service/TokenService.php

<?php

namespace App\Service;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
 * Class TokenService
 * @package App\Service
 */
class TokenService
{
    /** @var EntityManagerInterface $em */
    private $em;
    /** @var Request $request */
    private $request;

    /**
     * TokenService constructor.
     *
     * @param Request $request
     * @param EntityManagerInterface $em
     */
    public function __construct(Request $request, EntityManagerInterface $em)
    {
        $this->request = $request;
        $this->em = $em;
    }

    public function getTokens()
    {
        return true;
    }
}

谢谢!

我想我们已经有一段时间没有提出很好的请求堆栈问题了。我做了一些搜索,没有找到任何直接适用的答案并提供了一个体面的解释。

基本问题是Symfony 框架支持嵌套请求。例如,您在使用嵌入式控制器时会得到这些。所以没有实际的请求服务。实际上,在 Symfony 2.0 首次发布时曾经有过,但那真是一团糟。支持嵌套请求服务是在容器级别完成的,并不好玩。

于是大锤号称request stack was introduced to solve the problem once and for all. You inject the request stack代替request,真正需要的时候再访问request。

class TokenService
{
    private $em;
    private $requestStack;

    public function __construct(RequestStack $requestStack, EntityManagerInterface $em)
    {
        $this->requestStack = $requestStack;
        $this->em = $em;
    }

    public function getTokens()
    {
        $request = $this->requestStack->getMasterRequest(); // or possibly getCurrentRequest depending on where the tokens are
        return true;
    }

话虽如此,我个人只会传递来自控制器的请求。这样做可以摆脱我的 'it depends' 评论。我还认为它减少了 'magic' 所涉及的一点点。两种方法都有效。

class TokenService
{
    public function getTokens(Request $request)
    {
        return true;
    }
...
class TokenController
{
    public function showTokens(Request $request, TokenService $tokenService): JsonResponse
    {
        return $tokenService->getTokens($request);
    }