通过Symfony的BundleExtension类设置参数,而不是使用twitg-global


Setting parameters via BundleExtention class of Symfony rather than using twig global

因此,我正试图了解如何通过位于捆绑包的DependencyInjection文件夹中的BundleNameExtention类设置参数,而不是直接在config.yml 中定义参数

我使用的是symfony安装附带的默认AppBundle

在线阅读文档似乎很直接,在load方法中,我应该能够设置我想要的参数,所以这就是我所做的

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';
    }
}

如何在我的小树枝模板中访问此参数?

实际上,您必须在扩展类中实现use Symfony'Component'DependencyInjection'Extension'PrependExtensionInterface;接口,并添加以下prepend函数:

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'])));
    }
}

现在,您将可以在所有模板中访问app_comments

好吧,这就是我最终所做的。

在捆绑包的DependencyInjection文件夹和AppExtension类中,我还必须使用Configuration类来实现ConfigurationInterface Docs

我的AppExtension

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';
    }
}

这是我的Configuration

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;
    }
}

最后进入我的app/config/config.yml

app:
    comments: 'some value'

现在,要访问它,只需通过控制器获取它,并将其传递给trick模板。

$comments = $this->container->getParameter( 'app.comments' );
return $this->render('default/index.html.twig', array('comments' => $comments));

这对我来说很有效,希望它也能帮助其他人。