Getting the base url of the website and globally passing it to twig in Symfony 2

Geoffrey De Vylder picture Geoffrey De Vylder · Jul 27, 2011 · Viewed 211.2k times · Source

I'm making the switch from CodeIgniter to Symfony 2. Can someone please give me an example of how to:

  • Get the base url (the url without the route specific parts)
  • Globally pass this variable to the twig bundle so I can use it in every template.

Answer

codecowboy picture codecowboy · Jul 19, 2012

This is now available for free in twig templates (tested on sf2 version 2.0.14)

{{ app.request.getBaseURL() }}

In later Symfony versions (tested on 2.5), try :

{{ app.request.getSchemeAndHttpHost() }}