doupuchen6378 2015-08-30 10:50
浏览 19
已采纳

通过Symfony的BundleExtention类设置参数,而不是使用twig global

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?

  • 写回答

2条回答 默认 最新

  • dqrfdl5708 2015-09-01 10:41
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word