laravel Request::ip() code example
Example: laravel denny request by ip
//Run:
//php artisan make:middleware IpMiddleware
<?php
namespace App\Http\Middleware;
use Closure;
class IpMiddleware
{
public function handle($request, Closure $next)
{
if ($request->ip() != "192.168.0.155") {
return redirect('home');
}
return $next($request);
}
}
protected $routeMiddleware = [
'ipcheck' => \App\Http\Middleware\IpMiddleware::class,
];
then apply middelware to routes
Route::get('/', ['middleware' => ['ipcheck'], function () {
}]);