I'm using symfony 4.0 and created a custom config file. I'm setting this config file values to a parameter, so that I can get them in a controller. But I want to pass/access them also to twig. How could I do this? Is is possible to assign the app config directly in config.yml or could I do it with a twig extension? I found getGlobals(), but this is deprecated. Still did it with a twig function, but seems not a good way to me.
Best and thank you!
AppExtension.php:
namespace AppBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('config', $config);
}
}
?>
Resources/config/app.yml:
app:
document_access:
payment:
bank_details:
account_owner: xxx
iban: Iban
bic: Bic
ConfigExtension.php
namespace AppBundle\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfigExtension extends \Twig_Extension
{
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getFunctions(){
return array(
new \Twig_SimpleFunction('config', array($this, 'getConfig')),
);
}
public function getConfig()
{
return $this->container->getParameter('config');
}
}