rawat0419
rawat0419

Reputation: 136

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

I was trying to write kernel events in %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 }

and import that in app/config/config.yml

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

which is not working. But when I write all these settings in app/config/config.yml it works suddenly.

Is it even possible to do that or am I doing it wrong ??

Upvotes: 0

Views: 111

Answers (1)

Pi Wi
Pi Wi

Reputation: 1085

Is it a typo or did you forget to insert 2 (or 4) spaces in front of logging:?

And you import like this:

imports:
    - { resource: your_file }

The best solution is to use dependency injection to load the 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');
    }
}

This extension will load automatically and loads your services.yml.

Upvotes: 1

Related Questions