PHPackages                             laracrafts/laravel-geo-routes - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. laracrafts/laravel-geo-routes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

laracrafts/laravel-geo-routes
=============================

Geo location restricted routes for Laravel

v0.3.0(6y ago)109215[3 issues](https://github.com/LaraCrafts/laravel-geo-routes/issues)[1 PRs](https://github.com/LaraCrafts/laravel-geo-routes/pulls)MITPHPPHP ^7.1CI failing

Since Jan 17Pushed 6y ago9 watchersCompare

[ Source](https://github.com/LaraCrafts/laravel-geo-routes)[ Packagist](https://packagist.org/packages/laracrafts/laravel-geo-routes)[ RSS](/packages/laracrafts-laravel-geo-routes/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (5)Versions (7)Used By (0)

 [![](https://camo.githubusercontent.com/ea774f702afcbb1c575055331e64c6444e48f536bb93fe7479ff4c91b8c057b9/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f732f38656d347663756b6d776e736c65672f6c61726176656c2d67656f2d726f757465732d62616e6e65722e706e673f646c3d30)](https://camo.githubusercontent.com/ea774f702afcbb1c575055331e64c6444e48f536bb93fe7479ff4c91b8c057b9/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f732f38656d347663756b6d776e736c65672f6c61726176656c2d67656f2d726f757465732d62616e6e65722e706e673f646c3d30)

 [![](https://camo.githubusercontent.com/8cd283ec0429017e48a671cca4ae5f10eebfc708cf3e87c20134dbd87b46283e/68747470733a2f2f7472617669732d63692e6f72672f4c6172614372616674732f6c61726176656c2d67656f2d726f757465732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/LaraCrafts/laravel-geo-routes) [![](https://camo.githubusercontent.com/39fae831dbdbd30a1c4c87c1d0745f701d68c4605541e74414a97c160514085a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4c6172614372616674732f6c61726176656c2d67656f2d726f757465732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/LaraCrafts/laravel-geo-routes/) [![](https://camo.githubusercontent.com/4279effb9d311dd1514f23a6fd56da59f0b1673231fafc92f821df0c3ca894f7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4c6172614372616674732f6c61726176656c2d67656f2d726f757465732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/LaraCrafts/laravel-geo-routes/) [![](https://camo.githubusercontent.com/25ead3d51cd3fe29b80b177001fd8ad4b9ca01b90cb6cf453089427e43f0fc2b/68747470733a2f2f706f7365722e707567782e6f72672f6c6172616372616674732f6c61726176656c2d67656f2d726f757465732f762f737461626c65)](https://packagist.org/packages/laracrafts/laravel-geo-routes) [![](https://camo.githubusercontent.com/3a22ddfc44f68059a564e12c47e9ab1eb3435f03552c37360afdd76efbfe438a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c6172616372616674732f6c61726176656c2d67656f2d726f757465732e737667)](http://php.net/releases/) [![](https://camo.githubusercontent.com/6386690aa9ac2f27b90482689c6d7567a2ec3424f4e2f6aec7ad57ea5529c9cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c6172616372616674732f6c61726176656c2d67656f2d726f757465732e737667)](https://github.com/LaraCrafts/laravel-geo-routes/blob/master/LICENSE)

GeoLocation Restricted Routes For Laravel

Requirements
------------

[](#requirements)

- Laravel 5.5 or higher
- PHP 7.1 or higher

Installation
------------

[](#installation)

Navigate to your project's root folder via terminal or command prompt and execute the following command:

```
composer require laracrafts/laravel-geo-routes
```

> Note: If you are using [package discovery](https://laravel.com/docs/5.7/packages#package-discovery), you can skip the registration of the service provider and the alias as they do register themselves automatically, but don't forget to publish the configuration as described [below](#publish).

- Register the service provider

Open your `config/app.php` file and add this entry to the `providers` array

```
LaraCrafts\GeoRoutes\GeoRoutesServiceProvider::class,
```

- Publish the configuration

While still in the same folder, execute this command in your terminal:

```
php artisan vendor:publish --provider="LaraCrafts\GeoRoutes\GeoRoutesServiceProvider"
```

Usage
-----

[](#usage)

### Regular Routes

[](#regular-routes)

To get started real quick, the `allowFrom` and `denyFrom` methods allow you to restrict access to routes depending on *GeoLocations*

- Allow access from specific regions

```
Route::get('/home', 'FooController@bar')->allowFrom('us', 'gb');
```

What the above example does, is allowing access to the `/home` route **only** from the *United States* and the *United Kingdom*.

Alternatively we can do something like the following:

```
Route::get('/home', 'FooController@bar')->from('us', 'gb')->allow();
```

**By default,** all other countries will receive an **HTTP 401 Unauthorized Error**, to change this behavior you can use a callback as described in the [callbacks](#callbacks) section.

- Deny access from specific regions

So in the second example we are going to deny access **only** from specific locations, for instance: Canada, Germany and France

```
Route::get('/home', 'FooController@bar')->denyFrom('ca', 'de', 'fr');
```

Alternatively:

```
Route::get('/home', 'FooController@bar')->from('ca', 'de', 'fr')->deny();
```

> ***Note:*** This package uses *[ISO Alpha-2](https://www.nationsonline.org/oneworld/country_code_list.htm)* country codes.

> ***Note:*** This package uses [*stevebauman*](https://github.com/stevebauman)'s [location package](https://github.com/stevebauman/location), please refer to it's [official documentation](https://github.com/stevebauman/location/blob/master/readme.md) for a detailed guide on how to configure it correctly.

### Route Groups

[](#route-groups)

Besides allowing you to control access to regular routes, the ***`laravel-geo-routes`*** package also allows you to define route groups and do the same trick with them.

Please consider the following example:

```
Route::geo(['prefix' => '/en', function () {
    Route::get('/', 'HomeController@index');
    Route::get('/forums', 'ForumsController@index');
}])->allowFrom('dk', 'gb')->orRedirectTo('error');
```

> ***Note***: As you may have noticed, we are using the `geo` method instead of the default `group` method which will behave the same way the `group` method does, accepting an array of attributes for the first argument and a routes *closure* for the second one.

> ***Note***: Attributes methods can only be used after calling the `geo` method so instead of `Route::name('english.')->geo(...);` you have to write `Route::geo(...)->name('english.');`

> ***Note***: Unless a rule is applied using the `from`, `allow`, `deny`, `allowFrom` or `denyFrom` methods the route group will not be defined.

### Manual configuration

[](#manual-configuration)

Under the hood, the `allowFrom` and the `denyFrom` methods set the `geo` attribute on the routes which is an array containing the following parameters:

- \[array\] **`countries`**: The list of countries covered by the *geo-constraint*.
- \[string\] **`strategy`**: Determines whether to allow or deny access, the value can only be **allow** or **deny**.
- \[array\] **`callback`** (optional): The callback that will be invoked once the access is denied and its arguments.

Therefore, if you are more into verbosity, you can define your `GeoRoutes` in the following way:

```
Route::get([ 'geo' => ['countries' => ['us', 'ca'], 'strategy' => 'allow', 'callback' => [$myCallback, $myArgs]] ], function() {
    //
});
```

Your `GeoGroups` may also be defined manually as in the following example:

```
Route::group([ 'geo' => ['countries' => ['us', 'ca'], 'strategy' => 'allow', 'callback' => [$myCallback, $myArgs]] ], function() {
    //
});
```

Callbacks
---------

[](#callbacks)

As mentioned earlier, the default behavior for unauthorized users is an `HTTP 401 Unauthorized Error` response, but you are still able to change this behavior by using ***callbacks***.

To use a callback you have to simply add `->orCallback()` to the end of the GeoRoute constraint, like so:

```
Route::get('/forums', 'FooController@bar')
->allowFrom('de', 'ca')
->orCallback();
```

> ***Note:*** You can also mixin with native router methods

- ### Default Callbacks

    [](#default-callbacks)

*Laravel-geo-routes* has some useful built-in callbacks, we are going to list them below along with their use cases.

- `orNotFound`

The `orNotFound` callback will result in an HTTP 404 Not Found response for unauthorized visitors.

```
Route::get('/forums', 'FooController@bar')
->allowFrom('de', 'ca')
->orNotFound();
```

- `orRedirectTo`

This callback accepts one ***required*** argument which has to be a valid route name. Thanks to this callback, you'll be able to redirect unauthorized visitors to a route of your choice.

```
Route::get('/forums', 'FooController@bar')
->allowFrom('de', 'ca')
->orRedirectTo('myRoute');
```

- ### Custom callbacks

    [](#custom-callbacks)

The callbacks above might not be enough for your own use case, so you might want to add custom callbacks, in this guide we will go through several methods to define custom callbacks.

#### 1. Using a class

[](#1-using-a-class)

1. Create a new class, for instance `CustomCallbacks`
2. Add as many callbacks as you want to add, but be sure that all of your methods are **`static`** or you'll be facing problems
3. Open the `config/geo-routes.php` configuration file, and add your callbacks to the callbacks array, like so:

```
'callbacks' => [
    'myCallback' => 'CustomCallbacks::myCallback',
    'anotherCallback' => 'CustomCallbacks::anotherCallback'
]
```

Now your callbacks are ready, and you can start using them like so:

```
Route::get('/forums', 'FooController@bar')
->allowFrom('ca', 'us')
->orMyCallback();

Route::get('/blog', 'FooController@baz')
->denyFrom('fr', 'es', 'ar')
->orAnotherCallback();
```

> ***Notice*** that we have added the **`or`** prefix and converted the callback name to studly case (e.g. `myCallback` was converted to `orMyCallback`), be sure not to forget this note as it is very important for your callback to work.

You may also load these callbacks using the `parseCallbacks` method of the `CallbackRegistrar`.

**Example:**

```
use LaraCrafts\GeoRoutes\Support\Facades\CallbackRegistrar;

public function boot()
{
    CallbackRegistrar::parseCallbacks(MyCallbacksClass::class);
}
```

#### 2. Using an `array` of callbacks

[](#2-using-an-array-of-callbacks)

The `loadCallbacks` method allows you to load an associative array of callbacks.

**Example:**

```
use LaraCrafts\GeoRoutes\Support\Facades\CallbackRegistrar;

public function boot()
{
    $myCallbacksArray = [
        'handyName' => 'myClass::myCallback'
        //
    ]

    CallbackRegistrar::loadCallbacks($myCallbacksArray);
}
```

#### 3. Using the `callback` method

[](#3-using-the-callback-method)

The `callback` method allows you to add a single custom callback, accepting a name and a callable.

**Example:**

```
use LaraCrafts\GeoRoutes\Support\Facades\CallbackRegistrar;

public function boot()
{
    CallbackRegistrar::callback('foo', function () {
        return 'Sorry, you are not authorized.';
    });
}
```

Artisan Command
---------------

[](#artisan-command)

We have tweaked the `route:list` command and added new options in order to make retrieving the *geo-constraint* information easier. Below you will find the list of new options along with their usage examples.

OptionValueDescriptionUsage`--geo`|`-g`NoneShow the routes geo specifications`php artisan route:list --geo``--geo-only`NoneDisplay GeoRoutes only`php artisan route:list --geo-only``--country`String ([ISO Alpha-2 Country Code](https://www.nationsonline.org/oneworld/country_code_list.htm))Display only the routes that have a given country`php artisan route:list --country=[COUNTRY]``--strategy`String (allow|deny)Display only the routes that have a given strategy`php artisan route:list --strategy=[STRATEGY]`Contribution
------------

[](#contribution)

All contributions are welcomed for this project, please refer to the [CONTRIBUTING.md](https://github.com/LaraCrafts/laravel-geo-routes/blob/master/CONTRIBUTING.md) file for more information about contribution guidelines.

License
-------

[](#license)

**Copyright (c) 2019 LaraCrafts.**

This product is licensed under the MIT license, please refer to the [License file](https://github.com/LaraCrafts/laravel-geo-routes/blob/master/LICENSE) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 62.4% 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

Every ~80 days

Total

4

Last Release

2430d ago

PHP version history (2 changes)v0.1.0PHP ^7.1.3

v0.2.0PHP ^7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/f7991855cb00d8c378f7d1e33dca678b33d2d9ccfb52a33d1439d69859ad8c57?d=identicon)[yak0d3](/maintainers/yak0d3)

---

Top Contributors

[![yak0d3](https://avatars.githubusercontent.com/u/32985181?v=4)](https://github.com/yak0d3 "yak0d3 (108 commits)")[![axlon](https://avatars.githubusercontent.com/u/3661474?v=4)](https://github.com/axlon "axlon (62 commits)")[![vicgonvt](https://avatars.githubusercontent.com/u/7350307?v=4)](https://github.com/vicgonvt "vicgonvt (3 commits)")

---

Tags

geolocationlaravellaravel-packagelaravel-routesrouting

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/laracrafts-laravel-geo-routes/health.svg)

```
[![Health](https://phpackages.com/badges/laracrafts-laravel-geo-routes/health.svg)](https://phpackages.com/packages/laracrafts-laravel-geo-routes)
```

###  Alternatives

[wireui/wireui

TallStack components

1.8k1.3M16](/packages/wireui-wireui)[livewire/volt

An elegantly crafted functional API for Laravel Livewire.

4195.3M84](/packages/livewire-volt)[ramonrietdijk/livewire-tables

Dynamic tables for models with Laravel Livewire

21147.4k](/packages/ramonrietdijk-livewire-tables)

PHPackages © 2026

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