-
-
Save pije76/f45d922230fedf36f3800a6940cfe7b1 to your computer and use it in GitHub Desktop.
Symfony Event Listener to Increase Post View Count
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: bhaktaraz | |
* Date: 8/24/15 | |
* Time: 9:51 AM | |
*/ | |
namespace BRB\Bundle\PostBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
class PostController extends Controller | |
{ | |
public function viewAction() | |
{ | |
$event = new PostEvent($post); | |
$dispatcher = $this->get('event_dispatcher'); | |
$dispatcher->dispatch( | |
PostEvents::POST_VIEWED, | |
$event | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: bhaktaraz | |
* Date: 11/16/15 | |
* Time: 11:14 AM | |
*/ | |
namespace BRB\Bundle\ApiBundle\Event; | |
use BRB\Bundle\PostBundle\Entity\Post; | |
use Symfony\Component\EventDispatcher\Event; | |
class PostEvent extends Event | |
{ | |
protected $post; | |
public function __construct(Post $post) | |
{ | |
$this->post = $post; | |
} | |
public function getPost() | |
{ | |
return $this->post; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: bhaktaraz | |
* Date: 11/16/15 | |
* Time: 11:14 AM | |
*/ | |
namespace BRB\Bundle\ApiBundle\Event; | |
final class PostEvents | |
{ | |
const POST_VIEWED = 'post.viewed'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: bhaktaraz | |
* Date: 11/16/15 | |
* Time: 11:23 AM | |
*/ | |
namespace BRB\Bundle\ApiBundle\Listener; | |
use BRB\Bundle\ApiBundle\Event\PostEvent; | |
use Doctrine\ORM\EntityManager; | |
class PostListener | |
{ | |
/** | |
* | |
* @var EntityManager | |
*/ | |
protected $em; | |
public function __construct(EntityManager $entityManager) | |
{ | |
$this->em = $entityManager; | |
} | |
public function onPostViewed(PostEvent $event) | |
{ | |
$post = $event->getPost(); | |
$post->increaseViewCount(); // This method shoud be in your Post entity | |
$this->em->persist($post); | |
$this->em->flush(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
post_bundle.listener.post: | |
class: BRB\Bundle\ApiBundle\Listener\PostListener | |
arguments: [ "@doctrine.orm.entity_manager" ] | |
tags: | |
- { name: kernel.event_listener, event: post.viewed, method: onPostViewed } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment