Laravel Events | Laravel-Event Handling
What is Event Handling in Laravel?
- Event handling is the receipt of an event at some event handler from an event producer and subsequent processes.
- The processes involved in event handling include: Identifying where an event should be forwarded. Making the forward.
- An event is an action or occurrence recognized by a program that may be handled by the program.
- Laravel events simply provide an observer implementation.
Event can be handled by the following steps:
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
Step 1:
- Create an Event class.
- Event class can be created by executing the following command.
- Here the <event-class> should be replaced with the name of the event class.
- The created class will be stored at app\Events directory.
Step 2:
- Create a handler class to handle the created event.
- Event handler class can be created by executing the following command.
- Here the <event-class> should be replaced with the name of the event class that we have created in step-1 and the <handler-class> should be replaced with the name of the handler class.
- The newly created handler class will be stored at app\Handlers\Events directory.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
Step 3:
- Register the Event class and its handler in EventServiceProvider class.
- We now need to register the event and its handler class in app\Providers\EventServiceProvier.php file.
- This file contains an array called $listen.
- In this array, we need to add event class as key and event handler class as its value.
Step 4:
- Fire the event.
- Last step is to fire the event with Event facade. fire() method hsould be called which takes object of the event class.
Event can be fired as shown below:
- <Event Class Object> should be replaced with the object of the event class.
Example:
Step 1:
- Create a controller called CreateStudentController by executing the following command.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
Step 2:
- After successful execution, you will receive the following output -
Step 3:
- Copy the following code in app/Http/Controllers/CreateStudentController.php file.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
Step 4:
- Create an event called StudentAdded by executing the following command.
Step 5:
- After successful execution, you will receive the following output −
Step 6:
- The above command will create an event file at App\Events\StudentAdded.php. Copy the following code in that file.
App\Events\StudentAdded.php
Step 7:
- Create an event handler called HandleNewStudentAdded by executing the following command.
Step 8:
- After successful execution, you will receive the following output −
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
Step 9:
- The above command will create an event handler file at app\Handlers\Events\HandleNewStudentAdded.php. Copy the following code in that file.
app\Handlers\Events\HandleNewStudentAdded.php
Step 10:
- We now need to add the event class and its handler class in a file stored at app\Providers\EventServiceProvider.php.
- Notice the line in bold font and add that line in the file.
app\Providers\EventServiceProvider.php
Step 11 :
- Add the following lines in app/Http/routes.php.
app/Http/routes.php
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
Step 12:
- Visit the following URL to test the event.
- http://localhost:8000/event
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
Step 13:
- After visiting the above URL, you will receive the following output −
Step 14:
- Add the name of student and click the “Add student” button which will redirect you to the below screen. Look at the line highlighted in gray color.
- We have added this line in our handle method of HandleNewStudentAdded class which indicates that statements are executed in handle method when an event is fired.