Interrupting Applications with Laravel Middleware

Share this article

Image of Rocket
Laravel Course Image of Rocket
Before we launch into using middleware with Laravel, what is middleware? In general, middleware is software that connects different systems to one another. In Laravel, it’s one of the most well known and robust PHP frameworks that are available for use today, middleware provides a way for you to filter out your HTTP requests and manage those requests. This allows you to dictate how those requests interact with your application.

Why Do I Need Laravel’s Middleware?

The use of middleware in Laravel 5 can interrupt requests to your application. But that’s pretty opaque, isn’t it? How can that actually help you? If you’re already very familiar with Laravel, or the interaction of various software systems, you may already know this answer. If not, bear with us.

You Need Laravel’s Middleware to Interrupt Stuff

Laravel’s middleware can interrupt those requests and then perform various actions such as controlling access and requiring authentication to enter certain areas. So if you picture your application, it’s just an app, hanging out, and accepting incoming web requests and serving up your views, or API responses, or whatever else you’re delivering. But how does your application manage those requests? How does it filter them out? How does it know whether to check for your user credentials, or whether they’re authorized to do what the request is asking to do? Or how does your application know, based on the content of a request, what to do with that request specifically? The answer, of course, is middleware. Middleware can be used for authentication-related matters, but it can also be used for things like initiating logging, or for rate limits on your API. There are numerous use cases for middleware, and Laravel’s middleware allows you an elegant approach to handling these cases rather than trying to hack something together for each instance.

Artisan – Laravel’s Workhorse

And it really is the opposite of a hack. Laravel’s middleware is incredibly easy to use, in no small part because of Artisan, the command line interface tool that comes baked into Laravel 5. Artisan provides a simple way to create middleware, which can then be edited. You add your logic, and then you must configure it to be used by whichever routes you see fit, and presto, you now have working middleware.

Watch the Magic

TL;DR? This video will walk you through using middleware in your Laravel 5 application, and show you some practical use for it, using Laravel 5’s middleware documentation for reference. With Laravel’s middleware, you will be able to control the flow of requests to your application with ease! This video will also show you how to implement the middleware that you’ve constructed from your routes, so that requests to particular routes will now use the middleware that you have constructed and run that before anything further is done with the request. It’s simple, yet effective, and Laravel makes this process incredibly easy to handle.
Loading the player…

Still Hungry for More About Laravel 5?

We have a comprehensive Build a Blog with Laravel ready for you! Head on over to SitePoint to get started! This course will provide you with a firm understanding of Laravel 5 PHP development. It will give you the instructions you need to learn to install Laravel from scratch, use Laravel tools and methods, separate the various aspects of a Laravel application, and to handle the basic aspects of Create, Read, Update, and Delete (CRUD) system with Laravel. Your instructor, Isaac Castillo, has been working in the design and web application development fields for more than fifteen years. He works extensively with PHP, Laravel, WordPress, React Native, and other technologies, and previously a LAMP stack coding boot camp instructor. Neat, huh!

Frequently Asked Questions about Middleware in Laravel 5

What is the role of middleware in Laravel 5?

Middleware in Laravel 5 acts as a bridge between a request and a response. It is a type of filtering mechanism. For instance, Laravel includes a middleware that verifies if the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.

How can I create a new middleware in Laravel 5?

You can create a new middleware in Laravel 5 by using the Artisan command make:middleware. For example, if you want to create a middleware named CheckAge, you would use the command ‘php artisan make:middleware CheckAge’. This command will place a new CheckAge class within your app/Http/Middleware directory.

How can I register a new middleware in Laravel 5?

After creating a new middleware, you need to register it in your application before you can use it. You can register your middleware in the ‘app/Http/Kernel.php’ file. There are two types of middleware in Laravel: global and route middleware. Global middleware will run on every HTTP request of your application, while route middleware will only run when a specific route is hit.

How can I assign middleware to routes in Laravel 5?

You can assign middleware to specific routes in Laravel 5 by chaining the middleware method onto your route definition. For example, ‘Route::get(‘profile’, ‘UserController@show’)->middleware(‘auth’);’. This will assign the ‘auth’ middleware to the ‘profile’ route.

Can I use multiple middleware in Laravel 5?

Yes, you can assign multiple middleware to a route in Laravel 5. You just need to pass an array of middleware names to the middleware method. For example, ‘Route::get(‘/’, ‘HomeController@index’)->middleware(‘first’, ‘second’);’.

What is middleware groups in Laravel 5?

Middleware groups in Laravel 5 allow you to group several middleware under a single key, making them easier to assign to routes. Laravel comes with several middleware groups out of the box like web and api.

How can I create a middleware group in Laravel 5?

You can create a middleware group in Laravel 5 by adding your group to the $middlewareGroups property of your app/Http/Kernel.php class.

What is the purpose of the ‘Terminate’ middleware in Laravel 5?

The ‘Terminate’ middleware in Laravel 5 provides a convenient way to reduce the response time of your application by performing time consuming tasks after the response has been sent to the browser.

How can I use parameters in middleware in Laravel 5?

You can use parameters in middleware in Laravel 5 by defining them in your route action. For example, ‘Route::put(‘post/{id}’, function ($id) {})->middleware(‘role:editor’);’.

How can I handle exceptions in middleware in Laravel 5?

You can handle exceptions in middleware in Laravel 5 by using try/catch blocks within your middleware. If an exception is thrown within your middleware, you can catch it and handle it accordingly.

Jeff SmithJeff Smith
View Author

Jeff works for a startup as a technical writer, does contract writing and web development, and loves tinkering with new projects and ideas. In addition to being glued to a computer for a good part of his day, Jeff is also a husband, father, tech nerd, book nerd, and gamer.

Isaac CastilloIsaac Castillo
View Author

Isaac Castillo is a freelance web developer from San Antonio, Texas. Before becoming a freelancer, Isaac was a lead instructor, a local coding boot camp instructor teaching LAMP stack topics. He has even started his own teaching space at Isaac loves building applications with Laravel and JavaScript and has been working with design and web application development for over 15 years!

laravelSitepoint Premium
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week