Interrupting Applications with Laravel Middleware
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.
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!