Accessing Symfony global and server variables in Twig views through the “app” variable

In Symfony, you don’t always have to actually pass values from your controllers to your Twig views.  Twig can actually pull global and server values directly through the super global “app” variable.  Most frequently, you can access these corresponding objects like Kernel, Request, Security and Session variables like so:

{{app.kernel.cachedir}}
{{app.kernel.logdir}}
{{app.kernel.rootdir}}
 
{{app.request.basepath}}
{{app.request.baseurl}}
{{app.request.uri}}

{{app.security.token.user}}

{{app.session.locale}}

Need more Twig tips?  View our Symfony Twig Cheatsheet!

Looking for a PHP developer, a Symfony expert or WordPress web developer?  Perhaps a business system architect that specializes in scalable MySQL business system design and deployment?  We have reliable, fast and professional developers that meets your organization’s needs!  Contact us for your next web development project and schedule a free consultation today.

Aaron Belchamber has written 247 articles

Leave a Reply

Your email address will not be published. Required fields are marked *