Search code examples
phpsymfonyemailtokensymfony4

Symfony4 can't give token needed for route in email


I'm trying to reset a password with email, but I have the following error : An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("token") to generate a URL for route "resetPassword".").

here is the mail part from my controller :

$message = (new \Swift_Message('Reinitialisez votre mot de passe'))
                //put the email adress you defined in .env.local here
                ->setFrom('[email protected]')
                ->setTo($formEmail)
                ->setBody(
                    $this->renderView(
                        'emails/emailResetPassword.html.twig',
                        [
                            'name' => $name,
                            'token' => $token,
                        ]
                    ),
                    'text/html'
                );

            $mailer->send($message);

and here is the mail view :

<h1>Une demande de reinitialisation de mot de passe a été éffectuée</h1>

<p>Bonjour {{ name }}, cliquez <a href="{{ path('resetPassword') }}">ici</a> si vous avez demandé à réinitialiser votre mot de passe,
sinon vous pouvez ignorer ce email.</p>
<br>
<small>L'équipe.</small>

resetPassword is the following route :

/**
 * @Route("/changer-mot-de-passe/{token}", name="resetPassword")
 */

I think the problem is than Symfony doesn't understand than 'token' => $token is {token} from my route. But I don't know how to resolve my problem.

Thanks for your help :)


Solution

  • Symfony does not know what token to use if you don't provide it.

    You can pass parameters to the path() function, so your code should be:

    <p>Bonjour {{ name }}, cliquez <a href="{{ path('resetPassword', {'token': token}) }}">ici</a> si vous avez demandé à réinitialiser votre mot de passe,
    sinon vous pouvez ignorer ce email.</p>