1//Run:
2//php artisan make:middleware IpMiddleware
3
4<?php
5
6namespace App\Http\Middleware;
7
8use Closure;
9
10class IpMiddleware
11{
12
13 public function handle($request, Closure $next)
14 {
15 if ($request->ip() != "192.168.0.155") {
16 // here instead of checking a single ip address we can do collection of ips
17 //address in constant file and check with in_array function
18 return redirect('home');
19 }
20
21 return $next($request);
22 }
23
24}
25// END OF FILE
26
27// ---------------------
28// app/Http/Kernel.php
29//then add the new middleware class in the $middleware property of your app/Http/Kernel.php class.
30
31protected $routeMiddleware = [
32 //... other middlewares
33 'ipcheck' => \App\Http\Middleware\IpMiddleware::class,
34];
35// ------------------
36///In your route
37then apply middelware to routes
38
39Route::get('/', ['middleware' => ['ipcheck'], function () {
40 // your routes here
41}]);
42