Kamil P
Kamil P

Reputation: 784

Symfony add event listener

I use symfony and easy admin and I want to listen event easy_admin.pre_persist.

In easy admin controller it calls this event like this:

$this->dispatch(EasyAdminEvents::PRE_PERSIST, array('entity' => $entity));

and this consts:

/** @Event("Symfony\Component\EventDispatcher\GenericEvent") */
const PRE_PERSIST = 'easy_admin.pre_persist';

If I in the same controller add listener for this event like this:

    $ed = $this->get('event_dispatcher');

    $ed->addListener('easy_admin.pre_persist', function($e) {
        echo 'it works!';
        die();
    });

...it works.

But I want to add this listener in something other place. I think services.yml will be good place for it. I've read in Sf documentation i should add service this way:

# app/config/services.yml
services:
    app.exception_listener:
        class: AppBundle\EventListener\ExceptionListener
        tags:
            - { name: kernel.event_listener, event: kernel.exception }

but there is 2 properies - name and event. I know only event name, easy_admin.pre_persist.

How it works? What is name for this event and what is event? If I can add listener for easy_admin.pre_persist how can I add this listener to services.yml?

Upvotes: 1

Views: 3370

Answers (3)

BentCoder
BentCoder

Reputation: 12740

I'll give you a very simple example but you need to adapt it to yours. It should be fairly simple. It will just give you a fair idea, not a real life example.

For more examples: http://www.inanzzz.com/index.php/posts/symfony

UserController.php

class UserController
{
    ....

    public function createAction()
    {
        $user = new User();
        $user->setUsername('username');
        $user->setPassword('password');

        $this->entityManager->persist($user);
        $this->entityManager->flush();
    }

    ....
}

Services.yml

services:
    application_backend.listener.user_entity:
        class: Application\BackendBundle\Listener\UserEntityListener
        tags:
                - { name: doctrine.event_listener, event: prePersist }

UserControllerListener.php

namespace Application\BackendBundle\Listener;

use Application\BackendBundle\Entity\User;
use Doctrine\ORM\Event\LifecycleEventArgs;

class UserEntityListener
{
    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if ($entity instanceof User) {
            // Do whatever you want
        }
    }
}

Upvotes: 3

Edu
Edu

Reputation: 2520

You can see in the event definition you posted that use symfony generic events, so you should listen to the kernel and the tag name property should be kernel.event_listener

# app/config/services.yml
services:
    app.exception_listener:
        class: YourBundle\EventListener\EasydminPrePersitListener
        tags:
            - { name: kernel.event_listener, event: easy_admin.pre_persist, method: onPrePersist }

And in the Listener class you mus define a method onPrePersist()

Upvotes: 1

Massimiliano Arione
Massimiliano Arione

Reputation: 2466

You need to know the class of the listener and the name of the method to call. Both are depending on easy admin (sorry, never used it). So look into easy admin's code

Upvotes: 2

Related Questions