An open API service indexing awesome lists of open source software.

https://github.com/movemoveapp/laravel-maxmind

A Laravel package for reading MaxMind GeoIP2 data passed from Nginx via FastCGI parameters. Supports automatic detection of location and ISP information using predefined headers, with full configuration support.
https://github.com/movemoveapp/laravel-maxmind

Last synced: 3 months ago
JSON representation

A Laravel package for reading MaxMind GeoIP2 data passed from Nginx via FastCGI parameters. Supports automatic detection of location and ISP information using predefined headers, with full configuration support.

Awesome Lists containing this project

README

          

# 🌍 Laravel MaxMind
[![Latest Stable Version](http://poser.pugx.org/movemoveapp/laravel-maxmind/v)](https://packagist.org/packages/movemoveapp/laravel-maxmind)
[![Total Downloads](http://poser.pugx.org/movemoveapp/laravel-maxmind/downloads)](https://packagist.org/packages/movemoveapp/laravel-maxmind)
[![Latest Unstable Version](http://poser.pugx.org/movemoveapp/laravel-maxmind/v/unstable)](https://packagist.org/packages/movemoveapp/laravel-maxmind)
[![License](http://poser.pugx.org/movemoveapp/laravel-maxmind/license)](https://packagist.org/packages/movemoveapp/laravel-maxmind)
[![PHP Version Require](http://poser.pugx.org/movemoveapp/laravel-maxmind/require/php)](https://packagist.org/packages/movemoveapp/laravel-maxmind)

**Laravel MaxMind** is a simple and flexible package for accessing [MaxMind GeoIP2](https://maxmind.com/) data passed via Nginx FastCGI parameters.

It allows your Laravel application to read geolocation and ISP information that is resolved by Nginx using the GeoIP2 module, without querying MaxMind databases directly from PHP.

---

## Features

- Automatically parses FastCGI parameters set by Nginx with MaxMind GeoIP2 data
- Supports continent, country, region, city, postal, connection, and ISP data
- Configuration file to override default parameter names
- No need to access MaxMind database from PHP – uses data injected by Nginx

---

## Requirements

- Laravel 10+
- Nginx with `ngx_http_geoip2_module`
- MaxMind `.mmdb` database files
- FastCGI configuration passing GeoIP data to PHP

---

## Installation

```bash
composer require movemoveapp/laravel-maxmind
```

## Register the Service Provider
After installing the package, you need to register the MaxMind service provider.

### Laravel 10 and below

Add the following line to the providers array in your `config/app.php` file:

```php
...
'providers' => [
// Other providers...
MoveMoveApp\Maxmind\MaxmindServiceProvider::class,
],

```

### Laravel 11 and above

Register the service provider in the `bootstrap/providers.php` file:

```php
server('HTTP_X_CONTINENT_NAME'), etc. The package automatically maps them internally — you don't need to manually extract them in your controller.

### Verifying Integration

To verify that your headers are being received:

1. Run php artisan `route:clear` to refresh routing cache.
2. Create a debug route:

```php
Route::get('/debug-geo', function (\Illuminate\Http\Request $request) {
return request()->server();
});

```

3. Access `/debug-geo` in your browser and check for `HTTP_X_CONTINENT_NAME`, `HTTP_X_COUNTRY_NAME`, etc.

## Helpers

### Detect IP type

Then you can use the detectIpType function like this:

```php
use MoveMoveApp\Maxmind\Enums\NetworkType;

...

$ip = '2001:0db8:85a3::8a2e:0370:7334';

$type = detectIpType($ip);

if ($type === NetworkType::IPV6) {
echo "IPv6 address detected.";
} elseif ($type === NetworkType::IPV4) {
echo "IPv4 address detected.";
} else {
echo "Invalid IP address.";
}

```