Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed May 30, 2019
1 parent e26780b commit 8e2a323
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 17 deletions.
12 changes: 1 addition & 11 deletions src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,12 @@ public function commentNew(Request $request, Post $post, EventDispatcherInterfac
$em->persist($comment);
$em->flush();

// When triggering an event, you can optionally pass some information.
// For simple applications, use the GenericEvent object provided by Symfony
// to pass some PHP variables. For more complex applications, define your
// own event object classes.
// See https://symfony.com/doc/current/components/event_dispatcher/generic_event.html

// Since Symfony 4.3, it is better to use event object to make the calls to dispatch lighter
// https://symfony.com/blog/new-in-symfony-4-3-simpler-event-dispatching
$event = new CommentCreatedEvent($comment);

// When an event is dispatched, Symfony notifies it to all the listeners
// and subscribers registered to it. Listeners can modify the information
// passed in the event and they can even modify the execution flow, so
// there's no guarantee that the rest of this controller will be executed.
// See https://symfony.com/doc/current/components/event_dispatcher.html
$eventDispatcher->dispatch($event);
$eventDispatcher->dispatch(new CommentCreatedEvent($comment));

return $this->redirectToRoute('blog_post', ['slug' => $post->getSlug()]);
}
Expand Down
8 changes: 2 additions & 6 deletions src/Events/CommentCreatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
use App\Entity\Comment;
use Symfony\Contracts\EventDispatcher\Event;

/**
* The comment.created event is dispatched each time a comment is created
* in the system.
*/
class CommentCreatedEvent extends Event
{
protected $comment;
Expand All @@ -27,8 +23,8 @@ public function __construct(Comment $comment)
$this->comment = $comment;
}

public function getComment()
public function getComment(): Comment
{
return $this->comment;
}
}
}

0 comments on commit 8e2a323

Please sign in to comment.