Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
like image 916
Geoffrey De Vylder Avatar asked Jul 27 '11 07:07

Geoffrey De Vylder


1 Answers

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() }} 
like image 67
codecowboy Avatar answered Sep 18 '22 18:09

codecowboy