我放置了忘记密码的功能,为此,我要求用户填写他的电子邮件,然后我们向他发送一封带有令牌的电子邮件,以便他可以访问密码更改表单。 为此,我使用了TokenGeneratorInterface,但它向我显示了此错误:controller :: requestPassAction要求您为“ $ tokengenerator”参数提供一个值。该参数可以为空,并且没有提供空值,没有提供默认值,或者因为此参数之后有一个非可选参数。
控制器
public function requestPassAction(Request $request, TokenGeneratorInterface $tokenGenerator)
{
$email = $request->request->get('username');
$form = $this->createFormBuilder($email)
->add('username', EmailType::class)
->add('save', SubmitType::class, array('label' => 'Envoyer'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('DoctixUserBundle:User')->loadUserByUsername($form->getData()['username']);
// ->findOneBy(array('username' => $email));
if (!$user) {
$request->getSession()->getFlashBag()->add('warning', "Cet email n'existe pas.");
return $this->redirectToRoute("renouvellement_pass");
}
$user->setToken($tokenGenerator->generateToken());
// enregistrement de la date de création du token
$user->setPasswordRequestedAt(new \Datetime());
$em->flush();
$mailer = $this->get('mailer');
$message = (new \Swift_Message('Renouvellement du mot de passe'))
->setFrom("medmamtest@gmail.com")
->setTo($user->getUsername())
->setBody(
$this->renderView(
// app/Resources/views/Emails/registration.html.twig
'Resetting/request.html.twig',
array('user' => $user)
),
'text/html'
);
$mailer->send($message);
$request->getSession()->getFlashBag()->add('success', "Un mail va vous être envoyé afin que vous puissiez renouveller votre mot de passe. Le lien que vous recevrez sera valide 24h.");
return $this->redirectToRoute("login");
}
return $this->render('Resetting/request.html.twig', [
'form' => $form->createView()
]);
}
当我将参数初始化为null
public function requestPassAction(Request $request, TokenGeneratorInterface $tokenGenerator = null )
我有一个错误:在此行的null上调用成员函数generateToken():
$user->setToken($tokenGenerator->generateToken());
服务信息“ security.csrf.token_generator”
Option Value
---------------- --------------------------------------------
Service ID security.csrf.token_generator
Class Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator
Tags -
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired no
Autoconfigured no
服务
services:
_defaults:
autowire: true
Doctix\UserBundle\ArgumentResolver\UserValueResolver:
tags:
- { name: controller.argument_value_resolver, priority: 50 }
Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface: '@security.csrf.token_generator'
谢谢
答案 0 :(得分:1)
猜猜提示没有帮助。只需在您的services.yml文件中添加一个别名
# services.yml
Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface: '@security.csrf.token_generator'