在Symfony 3.4中开发迷你应用程序。我正在使用Guard整合身份验证过程。我创建了一个名为LoginFormAuthenticator的类,它扩展了AbstractFormLoginAuthenticator。
收到错误:
无法自动装配服务“app.security.login_form_authenticator”:参数“$ em”的方法 “AppBundle \ Security \ LoginFormAuthenticator :: __ construct()”引用 类“Doctrine \ ORM \ EntityManager”但不存在此类服务。尝试 将type-hint更改为其父级之一:interface “Doctrine \ ORM \ EntityManagerInterface”或接口 “教义\共同\持久性\的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是一个完整的新手如果我遗漏了一些明显的东西,请道歉。
答案 0 :(得分:1)
正如@Cerad在评论中指出的那样,您应该在构造函数中将EntityManager更改为EntityManagerInterface。
use Doctrine\ORM\EntityManager;
到
use Doctrine\ORM\EntityManagerInterface;
public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router)
到
public function __construct(FormFactoryInterface $formFactory, EntityManagerInterface $em, RouterInterface $router)
答案 1 :(得分:0)
Doctrine \ Common \ Persistence \ ObjectManager接口不再是doctrine.orm.entity_manager服务的别名,请改用 Doctrine \ ORM \ EntityManagerInterface 。