Reputation: 253
I put in place the functionality of the forgotten password, and for that I ask the user to fill in his email and we send him an email with a token so that he can access a password change form. And for that I use TokenGeneratorInterface, but it show me this error : controller::requestPassAction require that you provide a value for the "$tokengenerator" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.
Controller
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("[email protected]")
->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()
]);
}
And when I initialize the argument to null
public function requestPassAction(Request $request, TokenGeneratorInterface $tokenGenerator = null )
I have as error: Call to a member function generateToken() on null in this line :
$user->setToken($tokenGenerator->generateToken());
Information for Service "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
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'
Thanks
Upvotes: 0
Views: 262
Reputation: 48865
Guess the hints were not helping. Just add an alias to your services.yml file
# services.yml
Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface: '@security.csrf.token_generator'
Upvotes: 1