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.

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
Behzad Lashkari
  • 177
  • 1
  • 9
  • 14
    Change EntityManager to EntityManagerInterface in your constructor. – Cerad Feb 25 '18 at 21:36
  • 2
    Perfect! That worked, thank you! – Behzad Lashkari Feb 26 '18 at 14:07
  • 4
    Glad to be of help. Now, while it is still fresh, take a moment to understand why it works: https://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. – Cerad Feb 26 '18 at 14:12
  • 2
    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" – Stephan Vierkant Jun 27 '18 at 07:27
  • Because one does not simply trust error messages - community with explanation, for the win! – Milen Mar 10 '19 at 19:56

2 Answers2

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)
pagliuca
  • 1,129
  • 13
  • 19
  • 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. – pagliuca Apr 18 '19 at 00:35
1

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