How to create a middleware in Laravel?

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference
Search for a command to run...

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference
No comments yet. Be the first to comment.
Add it to the boot method of the AppServiceProvider of the app to improve Laravel developmet experience. use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; class AppServiceProvider extends ServiceProvider { //... pub...

There is a plugin available for reactjs that one can use to add Calendly to Nextjs i.e., react-calendly. The problem is if you want to customize the button to open popup or modal then you need to have Calendly's pro plan for it. Here is the alternati...

After shifting to Ubuntu from Windows, I constantly look up for the alternatives of the tools that are available in Windows to enhance my arsenal in Linux. DbGate is the smartest SQL+noSQL database client. It works on Windows, Linux, MacOS and in web...

<div className="ratio ratio-16x9"> <video width="900" height="650" muted playsInline autoPlay loop> <source src="/images/test.mp4" type="video/mp4" /> </video> </div>

Creating middleware in Laravel is a common task that helps to filter HTTP requests entering your application. Middleware can perform various tasks such as authentication, logging, and modifying request/response objects.
You can create middleware using the Artisan command-line tool. Open your terminal and run the following command:
php artisan make:middleware CheckAge
This will create a new middleware class named CheckAge in the app/Http/Middleware directory.
Open the newly created middleware file (app/Http/Middleware/CheckAge.php) and define the logic you want to apply. For example, you might want to check if the user is above a certain age:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class CheckAge
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if ($request->age <= 18) {
return redirect('home');
}
return $next($request);
}
}
In this example, the middleware checks if the age parameter in the request is less than or equal to 18. If so, it redirects the user to the home route. Otherwise, it allows the request to proceed further.
You need to register your middleware in the app/Http/Kernel.php file. Middleware can be registered globally, assigned to specific routes, or grouped.
To apply middleware to every request globally, add it to the $middleware array in the app/Http/Kernel.php file:
protected $middleware = [
// ...
\App\Http\Middleware\CheckAge::class,
];
If you want to use the middleware on specific routes, add it to the $routeMiddleware array:
protected $routeMiddleware = [
// ...
'check.age' => \App\Http/Middleware\CheckAge::class,
];
Once the middleware is registered, you can use it in your routes or controllers. Open the routes/web.php file and apply the middleware to specific routes:
Route::get('profile', function () {
// Only users older than 18 can access this route
})->middleware('check.age');
Or apply it to a group of routes:
Route::group(['middleware' => ['check.age']], function () {
Route::get('profile', function () {
// Only users older than 18 can access this route
});
Route::get('settings', function () {
// Only users older than 18 can access this route
});
});
You should test your middleware to ensure it works as expected. Create test cases or manually test different scenarios to verify the middleware's behavior.
To manually test, you can use a URL with different age query parameters to see if the redirection works:
httpCopy codehttp://your-app.test/profile?age=17 // This should redirect to 'home'
http://your-app.test/profile?age=19 // This should allow access to 'profile'