× Home About Gallery Blog Products Contacts Us
service_background

Product 2

In Symfony 3, we used to store parameters that we needed globally accessible in the parameters.yaml file, like the google analytics ID, the recaptcha id etc. However in Symfony 4, the introduction of the .env file moved all those parameters that we used to place in that file, to the new one. Till the date, for some developers it isn't so clear how to obtain these values now within a Twig view.

In this short article, we will show you 3 ways to obtain the value from your .env file using Twig in your Symfony 5 project.

Pass the parameter to the view from the controller

The most obvious solution to obtain a .env parameter inside a twig view is to simply pass it from the controller just as a regular variable:

 

Using a Twig Custom Function

The other option, which certainly suits better in most of the cases, is to create a new twig custom function that returns the environment variable on your views directly. Create the new extension if you don't have any created and register the new function get_env:

Loading…
Loading the web debug toolbar…
Attempt #