我试图在%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 中写下所有这些设置时,它会突然发挥作用。
甚至可以这样做,还是我做错了?
答案 0 :(得分:1)
这是拼写错误还是忘记在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。