AlanChavez AlanChavez - 3 months ago 18
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:

{{domainID}}/action/{{userId}}/anotherAction


And the latter URL points to:

http://localhost/viewA/{{domainID}}/action/{{userId}}/anotherAction


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

http://localhost/viewA/{{domainID}}/action/{{userId}}/anotherAction


it will point to:

http://localhost/viewB/{{domainID}}/action/{{userId}}/anotherAction


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?

EDIT

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

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 }) }}

http://symfony.com/doc/master/reference/twig_reference.html

Comments