'How to get an array global variable with symfony?
I have an entity 'Team' with an attribute 'country'. In my database, 'country' is the country's code (en, fr, it..). But I need, for my visitors, to display it as Country Name.
I put in my config/services.yaml this :
parameters:
countries:
'Afghanistan': 'af'
'Albanie': 'al'
'Allemagne': 'de'
...
It's working perfect for my forms, controllers... but I can't access it from my entities to display associate country name (with __toString()), so my visitors see the country's code and not the country's name. Is there a better way to deal with it ?
Solution 1:[1]
You should better use a service which takes over the processing of the data. The service can be injected in your controller. In the controller, you can call public methods of the service and the return-value can be used in the controller. The dependency injection concept is well explained here: https://symfony.com/doc/current/service_container.html
I can't recommend putting static data into Symfony this way. If there are a lot of countries involved, it will soon stop being funny.
For working with countries and locales, there is also a Symfony component ready-to-use, see https://symfony.com/doc/current/components/intl.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anton |
