Updated 4 August 2022
Most Common requirement of a website is to perform an action whenever any task or some event occurs. In this article, we are going to see how to create events and event listeners in bagisto.
For example – Send an email notification to the customer whenever the admin adds a new comment on any order.
1. Let’s suppose we have created a package and registered it into the composer.json file under psr-4.
1 2 3 4 5 |
"autoload": { "psr-4": { "App\\": "app/", "Webkul\\EventNotifications\\": "packages/Webkul/EventNotifications/src" } |
2. Create a listener for the package.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace Webkul\EventNotifications\Listeners; class Order { /** * Send order comment mail. * * @param \Webkul\Sales\Contracts\OrderComment $comment * @return void */ public function sendOrderCommentMail($comment) { $customerLocale = $this->getLocale($comment); if (! $comment->customer_notified) { return; } try { /** * Email to customer. */ $this->prepareMail($customerLocale, new OrderCommentNotification($comment)); } catch (\Exception $e) { report($e); } } } |
3. Create a provider which will extend the EventServiceProvider class and here we can listen to an event and trigger a listener whenever an event dispatch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php namespace Webkul\EventNotifications\Providers; use Illuminate\Foundation\Support\Providers\EventServiceProvider; class EventService extends EventServiceProvider { /** * The event handler mappings for the application. * * @var array */ protected $listen = [ 'sales.order.comment.create.after' => [ 'Webkul\EventNotifications\Listeners\Order@sendOrderCommentMail' ], ]; } |
4. Now Register the provider under config/app.php file and add the following line under ‘providers’.
1 |
Webkul\EventNotifications\Providers\EventProvider::class |
5. Finally dispatch the event from controller when a comment is done on any order by admin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php namespace Webkul\EventNotifications\Http\Controllers\Sales; use Illuminate\Support\Facades\Event; use Webkul\Admin\DataGrids\OrderDataGrid; use Webkul\Admin\Http\Controllers\Controller; use Webkul\Sales\Repositories\OrderRepository; use \Webkul\Sales\Repositories\OrderCommentRepository; class OrderController extends Controller { /** * Add comment to the order * * @param int $id * @return \Illuminate\Http\Response */ public function comment($id) { $data = array_merge(request()->all(), [ 'order_id' => $id, ]); // Checking if customer notified enabled or not. $data['customer_notified'] = isset($data['customer_notified']) ? 1 : 0; Event::dispatch('sales.order.comment.create.before', $data); $comment = $this->orderCommentRepository->create($data); // After comment dispatching event. Event::dispatch('sales.order.comment.create.after', $comment); } } |
So by this way, we can easily send a notification to customers for multiple events.
I’ve explored this while contributing to Laravel based project Bagisto, there are a lot more things to learn and you could also contribute to an open source E-Commerce framework.
Thanks for reading me. I hope you’ll get an idea how events and listener works with Laravel Please share your reviews on this, that will support me to write more.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.