如何从 symfony 中 app/config 中的包导入内核事件配置

How to import kernel events configuration from bundle in app/config in symfony

我试图在 %bundle%/config/services.yml

中编写内核事件
services:
logging:
    class:  AuditLoggerBundle\EventListener\AuditLogger
    arguments: [@service_container]
    tags:
            - { name: kernel.event_listener, event: oml.postInsert, method: postInsert, connection: default }
            - { name: kernel.event_listener, event: oml.preUpdate, method: preUpdate, connection: default }
            - { name: kernel.event_listener, event: oml.preDelete, method: preDelete, connection: default }
            - { name: kernel.event_listener, event: tm.preCommit, method: onTmPreCommit, connection: default }
            - { name: kernel.event_listener, event: tm.rollback, method: onTmRollback, connection: default }

并将其导入 app/config/config.yml

imports:
 resource: "@AuditLoggerBundle/Resources/config/services.yml"

这是行不通的。但是当我在 app/config/config.yml 中写下所有这些设置时,它突然起作用了。

是否可以这样做,还是我做错了??

是打错了还是忘记在logging:前面插入2(或4)个空格?

然后你这样导入:

imports:
    - { resource: your_file }

最好的解决方案是使用依赖注入来加载services.yml。

<?php
namespace AuditLoggerBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class AuditLoggerExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

此扩展程序将自动加载并加载您的 services.yml。