Skip to main content

Generating URLs

To generate a URL, you must specify the name of the route, and any parameters used in the path for that route.

Instead of hardcoding URLs into templates, the path Twig function can be used to generate URLs based on the routing configuration. Changing the route will then update all template instances where that route is used.

<a href="{{ path('welcome') }}">Home</a>

Further information on generating URLs is provided in the Symfony documentation - Generating URLs

<?php

$link->setUrl($this->generateUrl(
'news_article',
[
'articleId' => $newsArticle->getId(),
'articleSlug' => $newsArticle->getSlug(),
]
));