InvalidArgumentException Symfony4

InvalidArgumentException Symfony4

我将应用程序移至 docker 容器后出现问题。我是 Symfony 的新手,我不明白问题出在哪里。

在我想调用我的 UserController 索引操作后,我的浏览器抛出一个错误:

控制器 "App\Controller\UserController" 具有必需的构造函数参数,但容器中不存在。您是否忘记定义这样的服务? 函数 App\Controller\UserController::__construct() 的参数太少,在第 133 行 /projekty/taskit/vendor/symfony/http-kernel/Controller/ControllerResolver.php 中传递了 0 个参数,正好有 1 个预期

我的 services.yaml 文件:

parameters:
    locale: 'en'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    App\Entity\UserRepositoryInterface: '@App\DTO\UserAssembler'

UserController.php 文件

<?php

namespace App\Controller;

use App\Service\UserService;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
 * Class UserController
 * @package App\Controller
 */
class UserController extends AbstractController
{
    /**
     * @var UserService
     */
    private $userService;

    /**
     * @param UserService $userService
     */
    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    /**
     * @Route("/users/", name="users_index")
     */
    public function index()
    {
        $users = $this->userService->findAll();
        return $this->render('user/index.html.twig', array('users' => $users));
    }
}

还有我的UserService.php文件代码

<?php

namespace App\Service;

use App\Entity\UserRepositoryInterface;
use Doctrine\ORM\EntityNotFoundException;

/**
 * Class UserService
 * @package App\Service
 */
class UserService
{
    /**
     * @var UserRepositoryInterface
     */
    private $userRepository;

    /**
     * @param UserRepositoryInterface $userRepository
     */
    public function __construct(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    /**
     * @return array
     * @throws EntityNotFoundException
     */
    public function findAll() : array
    {
        $users = $this->userRepository->findAll();

        if (is_null($users)) {
            throw new EntityNotFoundException('Table users is empty');
        }
        return $users;
    }
}

在我看来你做了很多不必要的事情,这些事情已经是框架的一部分了。就像您的服务一样,它有什么作用?您可以直接从您的控制器调用您的存储库。在您的控制器中,您正在声明变量并构建这也是不必要的。只需在控制器的函数中使用依赖注入。这段代码应该可以工作并替换你的控制器和你的服务(同样,你可以去掉)。

<?php

namespace App\Controller;

use App\Repository\UserRepository;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
 * Class UserController
 * @package App\Controller
 */
class UserController extends AbstractController
{

    /**
     * @Route("/users/", name="users_index")
     */
    public function index(UserRepository $userRepository)
    {
        $users = $this->userRepository->findAll();
        return $this->render('user/index.html.twig', array('users' => $users));
    }
}