A simple Laravel package to block your application access by country.
- Block access to your Laravel app based on country.
- Using ipinfo.io to detect the user's country by their IP address.
- Easy configuration of blocked countries.
- Middleware-based approach, so it’s flexible and extendable.
You can install this package via Composer by running the following command in your Laravel project:
composer require avcodewizard/block-by-country
The package includes a configuration file where you can define which countries to block. The configuration file will be automatically published to your Laravel app. However, if you need to update or publish it manually, you can run:
php artisan vendor:publish --provider="Avcodewizard\BlockByCountry\BlockByCountryServiceProvider" --tag="config"
In the configuration file config/blockbycountry.php, you can specify the ISO country codes that you want to block.
return [
'blocked_countries' => ['CN', 'RU', 'IR'], // Add country codes to block here.
];
To block specific routes based on the user's country, simply apply the middleware block.country to any route or route group.
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware('block.country');
OR
Route::group(['middleware' => 'block.country'], function () {
Route::get('/dashboard', function () {
return view('dashboard');
});
// put all your routes inside this group ...
});
- PHP 7.4 or higher
- Laravel 8.x or higher