PHPackages                             aimsinfosoft/geo-restriction - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. aimsinfosoft/geo-restriction

ActivePackage[Security](/categories/security)

aimsinfosoft/geo-restriction
============================

restrict routes according to geographic locations

v1.0.0(1y ago)06MITPHP

Since Jan 8Pushed 1y ago1 watchersCompare

[ Source](https://github.com/aimsinfosoft/laravel-geographical-api-restriction)[ Packagist](https://packagist.org/packages/aimsinfosoft/geo-restriction)[ RSS](/packages/aimsinfosoft-geo-restriction/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Laravel-geographical-api-restriction package
============================================

[](#laravel-geographical-api-restriction-package)

The laravel geographical api restriction is a Laravel package that allows you to restrict access to api routes based on geographic locations. You can apply restrictions at the country or region level using customizable configurations.

Features
--------

[](#features)

- Restrict api routes by country or region.
- Middleware integration with Laravel.
- Customizable configurations for easy setup.

Installation
============

[](#installation)

To install the package, run the command:

```
composer require aimsinfosoft/geo-restriction
```

After installation, you need to publish the configuration file:

```
php artisan vendor:publish --tag=georestriction-config
```

After installation, you need to Change in `config/georestriction.php` file.

Locate the following line:

```
return [
    'restriction' => 1
    'allowed_countries' => ['IN','CA'],
    'blocked_countries' => ['CN'],
    'allowed_regions' => ['GJ', 'QC'],
    'blocked_regions' => ['GD'],
    'error_message' => 'Access denied due to geographical restrictions.'
];

```

Change it according to your preference:

#### 1. Set Restriction Type:

[](#1-set-restriction-type)

- restriction: Set to 0 for no restriction, 1 for country-based restrictions, or 2 for region-based restrictions.

#### 2. Allow or Block Countries:

[](#2-allow-or-block-countries)

- Use allowed\_countries to specify the countries you want to allow.
- Use blocked\_countries to specify the countries you want to block.

#### 3. Allow or Block Regions:

[](#3-allow-or-block-regions)

- If restriction is set to 2, use allowed\_regions for regions to allow and blocked\_regions for regions to block.

#### 4. Customize the Error Message:

[](#4-customize-the-error-message)

- Modify error\_message to set the message displayed to users in restricted areas.

Usage
-----

[](#usage)

### Routes Configuration

[](#routes-configuration)

After making the changes, you have to apply restrictions to your api routes, add the `geo.restriction` middleware in `routes/api.php` file

#### 1. Group Middleware

[](#1-group-middleware)

To apply the restriction to a group of api routes, use the middleware in your route file:

```
Route::middleware(['geo.restriction'])->group(function () {
    // Add all routes you want to apply restrictions to
});

```

#### 2. Single Route Middleware

[](#2-single-route-middleware)

To apply the restriction to individual api routes, use the `middleware()` method directly:

```
Route::get('/restricted-data', function () {
    return response()->json();
})->middleware('geo.restriction');

```

Example Workflow
----------------

[](#example-workflow)

```
1. Install the package using Composer.
2. Publish the configuration file.
3. Update config/geo-restriction.php with your restrictions:
    * Specify allowed or blocked countries/regions.
    * Choose the restriction type (0, 1, or 2).
    * Set the custom error message.
4. Apply the geo.restriction middleware to your routes as described above.

```

### Example

[](#example)

Here is example of restricting users based on their geographical locations.

After successfully adding package and publishing config file:

#### Step 1: Update the Configuration File

[](#step-1-update-the-configuration-file)

Open the `config/geo-restriction.php` file and configure the restrictions. For example:

```
return [
    'restriction' => 1,                     // Enable country-wise restriction
    'allowed_countries' => ['US', 'IN'],    // Allow users from the United States and India
    'blocked_countries' => ['CN'],          // Block users from China
    'allowed_regions' => [],                // As we apply restriction to country wise so make it empty
    'blocked_regions' => [],                // As we apply restriction to country wise so make it empty
    'error_message' => 'Access denied due to geographical restrictions.', // Custom error message
];

```

#### Step 2: Add API Routes with Middleware

[](#step-2--add-api-routes-with-middleware)

Apply the middleware to specific routes or groups in `routes/api.php` . For example:

```
// Group Middleware Example
Route::middleware(['geo.restriction'])->group(function () {
    Route::get('/user/profile', function (Request $request) {
        return response()->json([
            'message' => 'Access granted!',
            'user' => [
                'name' => 'Test',
                'email' => 'test@example.com',
            ],
        ]);
    });

    Route::get('/orders', function () {
        return response()->json([
            'message' => 'Here are your orders!',
        ]);
    });
});

// Single Route Middleware Example
Route::post('/user/update', function (Request $request) {
    return response()->json([
        'message' => 'Profile updated successfully!',
    ]);
})->middleware('geo.restriction');

```

#### Step 3: Test API Endpoints and See the Result

[](#step-3-test-api-endpoints-and-see-the-result)

1 From an allowed countries (e.g., United States, India (`US-IN`)):

- Access `GET /api/user/profile`: Returns a JSON response with the user's profile.
- Access `GET /api/orders`: Returns a JSON response with order details.
- Access `POST /api/user/update`: Returns a JSON response confirming the profile update.

Example Response for `/api/user/profile`:

```
{
    "message": "Access granted!",
    "user": {
        "name": "Test",
        "email": "test@example.com"
    }
}

```

2 From a blocked countries and except from allowed countries (e.g. China (`CN`)):

Example Response :

```
{
    "message": "Access denied due to geographical restrictions."
}

```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance41

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

486d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/852b0116b3ab96566377c0115cb2d1d69aaef95c42db94b10f7b285c99de2d17?d=identicon)[aimsinfosoft](/maintainers/aimsinfosoft)

---

Top Contributors

[![dhruvil-aims](https://avatars.githubusercontent.com/u/168068086?v=4)](https://github.com/dhruvil-aims "dhruvil-aims (8 commits)")

### Embed Badge

![Health badge](/badges/aimsinfosoft-geo-restriction/health.svg)

```
[![Health](https://phpackages.com/badges/aimsinfosoft-geo-restriction/health.svg)](https://phpackages.com/packages/aimsinfosoft-geo-restriction)
```

###  Alternatives

[defuse/php-encryption

Secure PHP Encryption Library

3.9k162.4M212](/packages/defuse-php-encryption)[roave/security-advisories

Prevents installation of composer packages with known security vulnerabilities: no API, simply require it

2.9k97.3M6.4k](/packages/roave-security-advisories)[mews/purifier

Laravel 5/6/7/8/9/10 HtmlPurifier Package

2.0k16.7M112](/packages/mews-purifier)[robrichards/xmlseclibs

A PHP library for XML Security

41278.1M118](/packages/robrichards-xmlseclibs)[bjeavons/zxcvbn-php

Realistic password strength estimation PHP library based on Zxcvbn JS

86917.5M63](/packages/bjeavons-zxcvbn-php)[enlightn/security-checker

A PHP dependency vulnerabilities scanner based on the Security Advisories Database.

33732.2M110](/packages/enlightn-security-checker)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
