AlanChavez AlanChavez - 1 year ago 97
Twig Question

In TWIG, is possible to get absolute url of a link with twig variables in it?

I have several URLs that look as follows:


And the latter URL points to:


However, If I try to load
through an iframe, the link inside
instead of pointing to:


it will point to:


and the user will end up in a 404 page if it follows the latter.

My question is:

Is there anyway to get the absolute path of a url built that way in twig?


The route definition is:

@Route("/domain/details/{domainId}", name="domain_detailed_view")

I tried to get the absolute path this way:

{{ url({{domainID}}/action/{{userId}}/anotherAction) }}

but I get this error:

A hash key must be a quoted string, a number, a name, or an expression
enclosed in parentheses

Answer Source

The url or path functions take the route name, not the path. You can give it an associative array as an optional second argument if the route requires parameters.

For example:

{{ url('domain_detailed_view', { 'domainId': domainId, 'userId': userId }) }}