In older versions of Symfony, we used to define parameters that didn't change on the deployment machine inside the config.yml
file:
# app/config/config.yml
# ...
parameters:
locale: en
framework:
# ...
# any string surrounded by two % is replaced by that parameter value
default_locale: "%locale%"
# ...
However, in Symfony 4, a lot of the variables that could be used in here moved to the env files or those that didn't change on the deployment machines, inside the services.yaml
file. For a lot of new developers on this version of the framework, is usually unclear how to retrieve those parameters in the most common places of the project like Controllers and Services, however we wrote how to retrieve them in this article. We didn't explain how to retrieve them from the Twig view. The first option is to expose them as global variables in Twig like this:
# config/packages/twig.yaml
twig:
# ...
globals:
ga_tracking: 'UA-xxxxx-x'
Then you can retrieve the values in Twig:
<p>The Google tracking code is: {{ ga_tracking }}</p>
However, this can be a problem for some of us because we need to add them in 2 places, in the services.yaml file and in the Twig Globals as well. Fortunately, there's a simple way to retrieve the parameters from a Twig view but having them within a single place.
In this short article, we will explain you how to retrieve easily those parameters from your Twig Views easily.
1. Be sure autowire is enabled and you have some parameters
In order to use the default way of retrieving parameters from your services.yaml
file, you will need to be sure that the autowire and autoconfigure properties are enabled in your project, you can check if its enabled in the same services.yaml
file where you can define parameters for your services:
# app/config/services.yaml
# Some retrievable parameters
parameters:
uploads_directory: '%kernel.project_dir%/public/uploads'
download_directory: '%kernel.project_dir%/public/downloads'
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
Knowing that these properties are enabled and you have some X parameters that need to be obtained from either services or controller, you are ready to follow the next step.
2. Create Twig Extension
In order to retrieve parameters from a Twig view, we will need to create a function in PHP that allow us to do so. You can create the TwigExtensions.php
file inside the app/src/Service
directory with the following content:
<?php
// app/src/Service/TwigExtensions.php
namespace App\Service;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class TwigExtensions extends \Twig_Extension
{
protected $params;
public function getFunctions()
{
return [
new \Twig_SimpleFunction('get_parameter', array($this, 'getParameter'))
];
}
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
public function getParameter($parameter)
{
return $params->get($parameter);
}
public function getName()
{
return 'TwigExtensions';
}
}
This extension, will add a new function to Twig, namely get_parameter
. This function will receive as first argument the parameter name as string and it will return it, that's it !
3. Retrieve parameter from Twig
Now in your Twig views you will be able to obtain the parameters from your services.yaml
file. For example, given the following parameters:
# app/config/services.yaml
parameters:
genres_image_directory: '%kernel.project_dir%/public/uploads/genres'
songs_image_directory: '%kernel.project_dir%/public/uploads/songs'
songs_directory: '%kernel.project_dir%/songs'
packages_directory: '%kernel.project_dir%/songs/package'
You can obtain them in your views like this:
{# Outputs: /var/www/vhosts/app/songs #}
{{ get_parameter("songs_directory") }}
{# Outputs: /var/www/vhosts/app/public/uploads/genres #}
{{ get_parameter("genres_image_directory") }}
Happy coding !