Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Event based development in Laravel

Friday, September 4th, 2015 by Servage

event-processWorking with events is a great way of handling actions in response to other actions. For example if a user makes a purchase, you want to store that purchase in your database, send a confirmation email and notify sales personel to handle the order. The order would be stored in the database right away, and the email is usually something your send right away or add to an email queue. Using a queue is better than sending immediately, because the website does not have to wait for the mail to send, but that is the topic for another article. Adding the notification to sales would also be something stored in the database.

In the code, you can break up the different steps into multiple event based parts. Say the emailing to customers is directly attached to ordering, but the notification to sales may be a policy defined somewhere else. So the actual order function does queue an email, but it does not actually create any notification to the sales staff. Instead if fires an event “OrderWasMade”, which looks up any other code that has subscribed to that event, and fires corresponding callbacks. Essentially this means that you can register callbacks (functions to be executed) with any event in your system. This is great because that way to you can add and control the behavior of your application.

Define events & listeners

You add events to the “listen” property of the “EventServiceProvider” class. This is a convenient and centralized way of storing all listeners. You get a good overview of which events and listernes are being used, which is very useful as your application grows.

protected $listen = [
    'App\Events\OrderWasMade' => [
        'App\Listeners\NotifySalesStaff',
    ],
];

The code is pretty straightforward, as the key of the “listen” array is an event class, and it defines an array of listener classes to execute.

Event class example

Below is an example event class for OrderWasMade.

<?php
namespace App\Events;

use App\Order;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;

class OrderWasMade extends Event
{
    use SerializesModels;

    public $podcast;

    public function __construct(Order $order)
    {
        $this->order = $order;
    }
}
?>

Listener class example

Below is a sample event class for NotifySalesStaff:

<?php
namespace App\Listeners;

use App\Events\OrderWasMade;

class NotifySalesStaff
{
    public function handle(OrderWasMade $event)
    {
	    // Create sales staff noticiation here ...
    }
}
?>

This example shows how easy Laravel lets you implement event driven development. Using this greatly improves understanding of an application’s workflows, by not hiding processes as direct results of class methods in a non-transparent chain of events.

Event based development in Laravel, 4.3 out of 5 based on 4 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.