- Lab
- Core Tech

Guided: Events, Services, and Middleware in Laravel
This code lab will take your Laravel knowledge to the next level with three separate intermediate-level concepts: events, service providers, and middleware.

Path Info
Table of Contents
-
Challenge
Create the Service Provider
In this lab, you will start with a basic time tracking app. The app’s features will be enhanced using events, service providers, and middleware. You will use a service provider to simplify reporting on weekly timesheets, middleware to block bad IP addresses, and events and listeners to automate an alert email for overtime.
You will create a service provider to handle time entries and calculate their summary. Laravel uses service providers to bind classes and functionality into the service container. The service container helps manage dependencies throughout the application. By creating a service provider, you're preparing the application to register services that can be reused across the application. In this case, you will create a provider to handle time entries efficiently. You will use the php artisan tool to generate the service, and edit the code as needed.
info> If you get stuck at some point while working through this lab, there are files containing the completed code for each task in the the
solution
folder. Now that the service provider is generated, you need to implement its functionality. The service provider will register a singleton of the TimeEntryService, meaning the application will share one instance of this service. This is a way to optimize resource usage and ensure consistency across the app when dealing with time entries.The register method binds the service to the container, making it available whenever it's needed. For example, when you need to calculate time entries, the TimeEntryService will be used to sum time values efficiently.
-
Challenge
Create Middleware for IP Filtering
Middleware in Laravel acts as a filter for HTTP requests entering your application. Here, you will create middleware to block certain IP addresses, adding an extra layer of security to the app. Middleware allows you to handle tasks like authentication, logging, or blocking requests based on IPs before the request reaches your application logic. You will build out all the logic, and implement it in the application.
-
Challenge
Create Events and Listeners
In this step you will be creating an event to handle time entry submissions. Events in Laravel allow you to decouple logic by triggering actions when something happens in the system. In this step, you'll create an event that triggers whenever time entries are updated. This allows other parts of the application (listeners) to react to this event, such as sending a weekly report.You will create a listener that sends a weekly report if a time entry is over 40 hours.
-
Challenge
Frontend View, Controller, and Route
Now that you have functionality to accept time entries and send weekly reports, you can build a simple frontend to see it work. You will create the route, controller, and view required to enter a time entry from a browser. In this Code Lab, you created and implemented services, middleware, and events in Laravel. You created a listener to send an email based on conditions of the event, and created logic to add time entries. This provides you with valuable understanding about managing event flows moving forward, and connecting them with user actions. Good luck on your upcoming development adventures!
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.