Kerni
Kerni

Reputation: 121

InvalidArgumentException Symfony4

I have a problem with my application after I moved it to docker container. I newbie with Symfony and I cannot understand where the problem is.

After I want to call my UserController index action my browser throws an error:

Controller "App\Controller\UserController" has required constructor arguments and does not exist in the container. Did you forget to define such a service? Too few arguments to function App\Controller\UserController::__construct(), 0 passed in /projekty/taskit/vendor/symfony/http-kernel/Controller/ControllerResolver.php on line 133 and exactly 1 expected

My services.yaml file:

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 file

<?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));
    }
}

And my UserService.php file code

<?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;
    }
}

Upvotes: 0

Views: 1349

Answers (1)

Dirk J. Faber
Dirk J. Faber

Reputation: 4691

It seems to me you are doing a lot of unnecessary things, which are already part of the framework. Like your service, what does it do? You can call your repositories directly from your controller. And in your controller, you are declaring variables and constructing which is also unnecessary. Just use dependency injection in your function in the controller. This little bit of code should work and replace both your controller and your service (which again, you can get rid of).

<?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));
    }
}

Upvotes: 1

Related Questions