Skip to content

Instantly share code, notes, and snippets.

@EuphoryX1
Last active December 17, 2015 00:59
Show Gist options
  • Save EuphoryX1/5525265 to your computer and use it in GitHub Desktop.
Save EuphoryX1/5525265 to your computer and use it in GitHub Desktop.
Symfony2 event
services:
euphory_storebundle.listener.comment:
class: Euphory\StoreBundle\Event\CommentListener
arguments: [@logger]
tags:
- { name: kernel.event_listener, event: euphory_storebundle.post.comment, method: onCommentEvent }
- { name: kernel.event_listener, event: euphory_storebundle.post.remove, method: onRemoveEvent }
<?php
namespace Euphory\StoreBundle\Event;
class CommentListener
{
protected $logger;
public function __construct($logger)
{
$this->logger = $logger;
}
public function onCommentEvent(CommentEvent $event)
{
$this->logger->info("onCommentEvent");
}
public function onRemoveEvent(CommentEvent $event)
{
$this->logger->info("onRemoveEent");
}
}
<?php
namespace Euphory\StoreBundle\Event;
use Symfony\Component\EventDispatcher\Event;
class CommentEvent extends Event
{
protected $post;
protected $comment;
public function __construct($post, $comment)
{
$this->post = $post;
$this->comment = $comment;
}
}
<?php
class DefeultController extends Symfony\Bundle\FrameworkBundle\Controller\Controller
{
public function dispatchAction()
{
$dispatcher = $this->get('event_dispatcher');
$dispatcher->dispatch('euphory_storebundle.post.comment', new \Euphory\StoreBundle\Event\CommentEvent($post, $comment));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment