crazydevil / ipfilter
IP access filter for Laravel applications
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires
- php: ^7.1.3
- illuminate/http: >=5.7.0 <5.9.0
- illuminate/support: >=5.7.0 <5.9.0
Requires (Dev)
- laravel/framework: >=5.7.0 <5.9.0
README
Laravel IP filter
This package provides a simple way to add an IP filter for your routes to restrict user access for your Laravel 5 application.
Install
Via Composer
$ composer require crazydevil/ipfilter
Laravel 5.5+ automatically register the service provider via auto-discover. With previous Laravel versions add the following line to your app.php
'providers' => [ ... CrazyDevil\Ipfilter\IpfilterServiceProvider::class, ... ]
Usage
Export the filter config file.
php artisan vendor:publish --provider="CrazyDevil\Ipfilter\IpfilterServiceProvider"
Add the middlware to your Kernel.php
protected $routeMiddleware = [ ... 'ipfilter' => CrazyDevil\Ipfilter\Ipfilter::class, ... ]
By default the filter is active only on the production environment. But you are able to add additional environments via the config file.
'environments' => ['production', 'staging'],
Filtering IPs
To allow IPs accessing your routes, you have to add them to the config file.
'allowed_ips' => [ '127.0.0.1', '10.0.0.1', ],
After adding the IP addresses you have to include the middlware to your routes.
Define middleware
For all web routes add the middleware to the function mapWebRoutes in RouteServiceProvider.php
protected function mapWebRoutes() { Route::middleware('web') ->middleware('ipfilter') ->namespace($this->namespace) ->group(base_path('routes/web.php')); }
For all api routes add the middleware to the function mapApiRoutes in RouteServiceProvider.php
protected function mapApiRoutes() { Route::prefix('api') ->middleware('api') ->middleware('ipfilter') ->namespace($this->namespace) ->group(base_path('routes/api.php')); }
For specific routes add the middleware to web.php or api.php
Route::get('/', function () { return view('welcome'); })->middleware('ipfilter');
It's also possible to accept or decline access to different routes. For example all routes should be accessable from a specified IP address except the Route /admin. To do that, you have to define this in the config file like this example: config/ipfilter.php
'allowed_ips' => [ '127.0.0.1', '10.0.0.1', ], 'routes' => [ 'admin' => [] ]
web.php or api.php
Route::get('/', function () { return view('welcome'); })->middleware('ipfilter'); Route::get('/admin', function () { return view('admin'); })->middleware('ipfilter:admin');
In this example the IP addresses 127.0.0.1 and 10.0.0.1 can access all routes in your application except the route /admin
Credits
License
The MIT License (MIT). Please see License File for more information.