Symfony 3.4 autowire service
Asked Answered
T

2

13

Am developing a mini app in Symfony 3.4. Am putting together an authentication process using Guard. I have created a class called LoginFormAuthenticator which extends AbstractFormLoginAuthenticator.

Receiving error:

Cannot autowire service "app.security.login_form_authenticator": argument "$em" of method "AppBundle\Security\LoginFormAuthenticator::__construct()" references class "Doctrine\ORM\EntityManager" but no such service exists. Try changing the type-hint to one of its parents: interface "Doctrine\ORM\EntityManagerInterface", or interface "Doctrine\Common\Persistence\ObjectManager".

My code in my form authenticating class:

    <?php

namespace AppBundle\Security;


use AppBundle\Form\LoginForm;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;

class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
    private $formFactory;
    private $em;
    private $router;

    public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router)
    {

        $this->formFactory = $formFactory;
        $this->em = $em;
        $this->router = $router;
    }

    public function getCredentials(Request $request)
    {
        $isLoginSubmit = $request->getPathInfo() == '/login' && $request->isMethod('POST');

        if(!$isLoginSubmit){
            return false;
        }

        $form = $this->formFactory->create(LoginForm::class);
        $form->handleRequest($request);

        $data = $form->getData();
        return $data;
    }

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $username = $credentials['_username'];

        return $this->em->getRepository('AppBundle:User')
            ->findOneBy(['email' => $username]);
    }

    public function checkCredentials($credentials, UserInterface $user)
    {
        $password = $credentials['_password'];
        if($password == 'iliketurtles'){
            return true;
        }
        return false;
    }

    protected function getLoginUrl()
    {
        return $this->router->generate('security_login');
    }
}

My services.yml:

services:
# default configuration for services in *this* file
_defaults:
    # automatically injects dependencies in your services
    autowire: true
    # automatically registers your services as commands, event subscribers, etc.
    autoconfigure: true
    # this means you cannot fetch services directly from the container via $container->get()
    # if you need to do this, you can override this setting on individual services
    public: false

# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
    resource: '../../src/AppBundle/*'
    # you can exclude directories or files
    # but if a service is unused, it's removed anyway
    exclude: '../../src/AppBundle/{Entity,Repository,Tests}'

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
    resource: '../../src/AppBundle/Controller'
    public: true
    tags: ['controller.service_arguments']

# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
#     arguments:
#         $someArgument: 'some_value'

app.security.login_form_authenticator:
    class: AppBundle\Security\LoginFormAuthenticator
    autowire: true 

Am a complete novice in Symfony so apologies if I'm missing something obvious.

Tinaret answered 25/2, 2018 at 21:22 Comment(5)
Change EntityManager to EntityManagerInterface in your constructor.Wenzel
Perfect! That worked, thank you!Tinaret
Glad to be of help. Now, while it is still fresh, take a moment to understand why it works: symfony.com/doc/current/service_container/autowiring.html You won't get very far with S4 without at least a basic understanding of autowire and the service container. And "bin/console debug:container --show-private" should become one of your best friends.Wenzel
I don't get why this question is upvoted. The answer is mentioned in the error message: "Try changing the type-hint to one of its parents: interface "Doctrine\ORM\EntityManagerInterface"Recriminate
Because one does not simply trust error messages - community with explanation, for the win!Deaconess
L
8

As noted by @Cerad in the comments, you should change EntityManager to EntityManagerInterface in your constructor.

Change the line

use Doctrine\ORM\EntityManager;

to

use Doctrine\ORM\EntityManagerInterface;

And also change the line

public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router)

to

public function __construct(FormFactoryInterface $formFactory, EntityManagerInterface $em, RouterInterface $router)
Lepage answered 18/4, 2019 at 0:34 Comment(1)
Guys, this question was already answered in the comments, but I decided to answer it "for real" so it stops showing up on the "Unanswered questions" search.Lepage
W
1

The Doctrine\Common\Persistence\ObjectManager interface is no longer aliased to the doctrine.orm.entity_manager service, use Doctrine\ORM\EntityManagerInterface instead.

Wallace answered 6/12, 2019 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.