Search code examples
phpsymfonytwigphpmailer

getting complete url in email template TWIG


I'm working on an app using Symfony 6 and I installed the reset-password-bundle. Everything is working perfectly except when I get the email asking to change the password. The redirection in TWIG via the "url" function doesn't redirect to the correct url. When I click on the link in the email the url sends me to localhost/mypagepath/token which gives me a 404. When I write https://localhost:8000/mypagepath/token it works and I get the changing password form. I don't understand why the url twig function doesn't include the scheme and the port . I'm running my app in dev environment with the symfony server.

Here is the line in twig. I didn't change that this is the template by default in the bundle.

<p>To reset your password, please visit the following link</p>

<a href="{{ url('app_reset_password', {token: resetToken.token}) }}">{{ url('app_reset_password', {token: resetToken.token}) }}</a>

<p>This link will expire in {{ resetToken.expirationMessageKey|trans(resetToken.expirationMessageData, 'ResetPasswordBundle') }}.</p>

I tried to change the default uri in the routing.yaml file

default_uri: https://localhost:8000

It added the https but not the port so the link still doesnt work.

I also tried to change url function to absolute_url.

Thank you in advance for any help.


Solution

  • Here is a specific tutorial from symfony about your topic. https://symfonycasts.com/screencast/mailer/absolute-urls (min 3:20)

    I really recommend to watch it because it covers exactly your case.

    To make it short: How about using { absolute_url() }? I know that you tried it already but it's important to understand what the difference between path(), url() and absolute_url() is.

    I also recommend to revert your change to the route.yaml and try it again.

    Symfony is detecting the port and the URL out of the request/response context. You can use the symfony debugging toolbar to check if all information is in the right place.