Symfony 5.1 – Cannot autowire service

I want to refactoring my code, but now i have error and don’t understand what.
Objectif : Don’t need to pass parameters when call TokenService, and use autowiring to autowiring EntityManager & Request, and don’t set it when controller call service.

Cannot resolve argument $tokenService of AppControllerTokenController::showTokens()
Cannot autowire service AppServiceTokenService
argument $request of method __construct() references class SymfonyComponentHttpFoundationRequest but no such service exists.

Before :

/src/Controller/TokenController.php

<?php

namespace AppController;

use AppServiceTokenService;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentHttpFoundationRequest;

/**
 * @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 AppService;

use DoctrineORMEntityManagerInterface;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;

/**
 * Class TokenService
 * @package AppService
 */
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;
    }
}

After :

/config/services.yaml

parameters:

services:
    _defaults:
        autowire: true
        autoconfigure: true

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

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

    AppServiceTokenService: ~

/src/Controller/TokenController.php

<?php

namespace AppController;

use AppServiceTokenService;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentHttpFoundationRequest;

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

/src/Service/TokenService.php

<?php

namespace AppService;

use DoctrineORMEntityManagerInterface;
use SymfonyComponentHttpFoundationRequest;
/**
 * Class TokenService
 * @package AppService
 */
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;
    }
}

Thanks !

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *