So I am trying to wrap my head around how to set parameters via the BundleNameExtention
class that resides inside the DependencyInjection
folder of a bundle rather than defining them directly in config.yml
I am using a default AppBundle
that came with symfony install.
It seems pretty straight forward reading the documentation online that inside the load
method I should be able to set the parameters I want so this is what I did
namespace AppBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\xmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$config['comments'] = "some value";
$container->setParameter('app.comments', $config['comments']);
}
public function getAlias(){
return 'app';
}
}
How can I access this parameter in my twig template?
Okay so this is what I ended up doing to achieve it.
Inside the DependencyInjection
folder of a bundle along with AppExtension
class I had to use Configuration
class as well which implements ConfigurationInterface
Docs
My AppExtension
class
namespace AppBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\xmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('app.comments', $config['comments']);
}
public function getAlias()
{
return 'app';
}
}
This is my Configuration
class
namespace AppBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('app');
$rootNode->children()->scalarNode('comments')->end();
return $treeBuilder;
}
}
And finally inside my app/config/config.yml
app:
comments: 'some value'
Now to access this simply fetch it via controller and pass it to the twig template.
$comments = $this->container->getParameter( 'app.comments' );
return $this->render('default/index.html.twig', array('comments' => $comments));
This works for me, hopefully it will help someone else too
You can actually, you'll have to implement the use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
interface in your extension class, and add the following prepend
function:
class AppExtension extends Extension implements PrependExtensionInterface{
// ... your other code
public function prepend(ContainerBuilder $container){
$configs = $container->getExtensionConfig($this->getAlias());
$config = $this->processConfiguration(new Configuration(), $configs);
$container->prependExtensionConfig('twig', array(
'globals' => array('app_comments', $config['comments'])));
}
}
Now you will have access to app_comments
in all your templates.