Instead of hard coding the parameters in parameters.yml
I am trying to load them from database. Not all parameters in parameters.yml needs to be loaded from dat
The solution with CompilerPass
didn't work in my case in Symfony 4, it worked only from the controller but not when used in the twig.yaml file:
param_name: '%param_name%'
Since I need to use this parameter in the sidebar, so in all the pages, passing it from the controller is not feasible.
This is what I did, thanks to the fact that you can reference services as global Twig variables.
I have a service, called Settings, that loads the settings directly from the database, simplified:
class Settings
{
/** @var \PDO */
private $db;
public function __construct(EntityManagerInterface $em)
{
$this->db = $this->em->getConnection();
}
/**
* Gets a setting value.
* If the setting doesn't exist, returns the default value specified as second param
*/
public function get(string $name, $default=''): string
{
$stmt = $this->db->prepare("SELECT `value` FROM `settings` WHERE `key`=?;");
$stmt->execute([$name]);
return $stmt->fetchColumn() ?: $default;
}
/**
* Sets a setting value.
* If the setting doesn't exists, it creates it. Otherwise, it replaces the db value
*/
public function set(string $name, string $value, string $description = '')
{
$this->db->prepare("INSERT INTO settings (`key`, `value`, `description`) VALUES (?,?,?) ON DUPLICATE KEY UPDATE `value`=?;")
->execute([$name, $value, $description, $value]);
}
Now, I simply added in config/packages/twig.yaml
:
twig:
# ...
globals:
# ...
settings: '@App\Service\Settings'
This is amazing because now I can read any db setting from any template:
{{ dump(settings.get('setting_name')) }}
The Settings class I posted is simplified, it can still be improved for example to avoid making the same query again if you request the same setting more than once, but that's trivial to do (store the fetched keys in a private $fetched
array)