The Laravel Block IP package provides a convenient way to control everything related to website visits using the Laravel framework. It allows you to block specific IP addresses, configure rate limiting, and receive notifications for certain events.
You can install the package via Composer by running the following command:
composer require michaelnabil230/laravel-block-ipAfter installing the package, you need to publish the package files and run the migrations:
php artisan block-ip:install
php artisan migrateTo publish and run the migrations separately, you can use the following commands:
php artisan vendor:publish --tag="laravel-block-ip-migrations"
php artisan migrateYou can also publish the package's configuration file using the following command:
php artisan vendor:publish --tag="laravel-block-ip-config"The published configuration file allows you to customize various settings related to block IP functionality, notifications, caching, and more.
To configure rate limiting and use the package's functionality, you can add code to your app/Providers/RouteServiceProvider.php file. The following code demonstrates how to configure rate limiting and enable the package:
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
\MichaelNabil230\BlockIp\BlockIpRegistrar::rateLimiter();
}By default, the rate limiter is set to allow 60 requests per minute, but you can customize this value by passing it to the rateLimiter() method.
The package provides a BlockIpMiddleware middleware that you can add to your routes in the app/Http/Kernel.php file. Make sure to uncomment the necessary line for the throttle middleware as well. Here's an example:
protected $middlewareAliases = [
// ...
'block-ip' => \MichaelNabil230\BlockIp\Middleware\BlockIpMiddleware::class,
];To protect your routes using the block IP middleware and rate limiting, you can define them as follows:
Route::middleware(['block-ip', 'throttle:block-ip'])->group(function () {
// Your protected routes here
});If you want to unblock all IP addresses, you can use the following command:
php artisan block-ip:unblock --allYou can also unblock multiple IP addresses by providing them as a comma-separated list:
php artisan block-ip:unblock --ips=127.0.0.1,127.0.0.2To block new IP addresses, you can use the following command:
php artisan block-ip:block 127.0.0.1 127.0.0.2If you find this package useful, you can show your support by contributing financially:
You can run the package's tests using the following command:
composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.


