Symfony 3.4自动服务

问题描述 投票:6回答:1

我正在开发Symfony 3.4中的迷你应用程序。我正在使用Guard整合身份验证过程。我创建了一个名为LoginFormAuthenticator的类,它扩展了AbstractFormLoginAuthenticator。

收货错误:

无法自动装配服务“app.security.login_form_authenticator”:方法“AppBundle \ Security \ LoginFormAuthenticator :: __ construct()”的参数“$ em”引用类“Doctrine \ ORM \ EntityManager”但不存在此类服务。尝试将type-hint更改为其父项之一:interface“Doctrine \ ORM \ EntityManagerInterface”,或接口“Doctrine \ Common \ Persistence \ ObjectManager”。

我的表单中的代码验证类:

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

我的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 

在Symfony是一个完整的新手,如果我遗漏了一些明显的东西,请道歉。

symfony autowired symfony-3.4
1个回答
1
投票

正如@Cerad在评论中所指出的,您应该在构造函数中将EntityManager更改为EntityManagerInterface。

Change the line

use Doctrine\ORM\EntityManager;

use Doctrine\ORM\EntityManagerInterface;

And also change the line

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

public function __construct(FormFactoryInterface $formFactory, EntityManagerInterface $em, RouterInterface $router)
© www.soinside.com 2019 - 2024. All rights reserved.