Linking to routes in Twig has become much easier since the release of some updates in Drupal 8. Here are 5 snippets that help when linking to internal routes.

{# Link to frontpage view. #}
<a href="{{ path('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a>

{# Link to user entity/profile page. #}
<a href="{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a>

{# Link to node page. #}
<a href="{{ path('entity.node.canonical', {'node': node.id}) }}">{{ 'View node page'|t }}</a>

{# Add a destination query parameter. Simple Example #}
<a href="{{ path('user.login', {}, {'query': {'destination': path('<current>') }}) }}">{{ 'View node page'|t }}</a>.

{# Add a destination query parameter. Advanced Example, event_user_sync.registration is a custom route from a custom module with the name event_user_sync #}
{% set redirect_path = path('event_user_sync.registration', {'node': node.id}) %}
<a href="{{ path('user.register',{},{'query':{'destination': redirect_path }}) }}">{{ 'Register'|t }}</a>

Make sure you clear caches so new routes ar registered when adding them in your module.routing.yml files.

More functions here.

Common error: https://stefvanlooveren.me/errors/how-fix-symfonycomponentroutingexceptionroutenotfoundexception-route-does-not-exist

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!