4

I want to avoid hard-coding relative URLs in my twig templates in a Symfony2 project.

I have defined some route names in my controllers, for example:

@Route("/", name="homepage")

Is there a Twig or Symfony2 function that could help me generate links properly? Something like:

<a href='{{ magic_fct("homepage") }}'>Home</a>

would return:

<a href='/'>Home</a>

2 Answers 2

8

Use following:

<a href='{{ path('homepage') }}'>Home page</a>
Sign up to request clarification or add additional context in comments.

Comments

6

Yes, of course you can. Take a look in Linking to Pages documentation.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.