PHPackages                             huluti/geoip2-bundle - 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. huluti/geoip2-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

huluti/geoip2-bundle
====================

A Symfony Bundle for the Maxmind GeoIP2 API

3.0.0(3mo ago)13.0k↑59%MITPHPPHP &gt;=8.1.0CI passing

Since Mar 3Pushed 3mo agoCompare

[ Source](https://github.com/Huluti/geoip2-bundle)[ Packagist](https://packagist.org/packages/huluti/geoip2-bundle)[ Docs](https://github.com/huluti/geoip2-bundle)[ RSS](/packages/huluti-geoip2-bundle/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)Dependencies (13)Versions (2)Used By (0)

*This is a fork of [gpslab/geoip2](https://github.com/gpslab/geoip2), maintained for newer Symfony versions.*

[![Latest Stable Version](https://camo.githubusercontent.com/94b18a07d040be573a3a79190bd635284106b91caa3a7b713861665b569e0aef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68756c7574692f67656f6970322d62756e646c652e7376673f6d61784167653d33363030266c6162656c3d737461626c65)](https://packagist.org/packages/huluti/geoip2-bundle)[![Test](https://github.com/huluti/geoip2/actions/workflows/test.yaml/badge.svg)](https://github.com/huluti/geoip2-bundle/actions/workflows/test.yaml)[![License](https://camo.githubusercontent.com/e0db9c3c4a1b5e4af721bc41c9ae705976cd87c382357c366106c94507237034/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f68756c7574692f67656f6970322d62756e646c652e7376673f6d61784167653d33363030)](https://github.com/huluti/geoip2-bundle)

A Symfony Bundle for the Maxmind GeoIP2 API
===========================================

[](#a-symfony-bundle-for-the-maxmind-geoip2-api)

Bundle to use [maxmind/GeoIP2](https://github.com/maxmind/GeoIP2-php) in Symfony.

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

[](#installation)

Pretty simple with [Composer](http://packagist.org), run:

```
composer require huluti/geoip2-bundle
```

Configuration
-------------

[](#configuration)

To configure auto-update the database you need to generate your personal licence key.

#### Steps for generate licence key

[](#steps-for-generate-licence-key)

1. [Sign up for a MaxMind account](https://www.maxmind.com/en/geolite2/signup) (no purchase required)
2. Login and generate a [licence key](https://www.maxmind.com/en/accounts/current/license-key)
3. Save your licence key
4. Open [download page](https://www.maxmind.com/en/download_files) and find your needed DB edition `ID` and copy value from first column.

Example configuration:

```
huluti_geoip:
    # Your personal licence key
    license: 'XXXXXXXXXXXXXXXX'

    # One of database edition IDs:
    #   GeoLite2-ASN
    #   GeoLite2-City
    #   GeoLite2-Country
    #   GeoIP2-City
    #   GeoIP2-Country
    #   GeoIP2-Anonymous-IP
    #   GeoIP2-Domain
    #   GeoIP2-ISP
    edition: 'GeoLite2-City'
```

#### Database source URL

[](#database-source-url)

By default, this URL is used to download a new databases `https://download.maxmind.com/app/geoip_download?edition_id={edition_id}&license_key={license_key}&suffix=tar.gz`

- `edition_id` - character ID name from first column on [download page](https://www.maxmind.com/en/download_files);
- `license_key` - your personal [licence key](https://www.maxmind.com/en/accounts/current/license-key).

You can change this URL, for example, if you want to use a proxy to download the database. You can customize the source URL in the configuration.

```
huluti_geoip:
    license: 'XXXXXXXXXXXXXXXX'
    edition: 'GeoLite2-City'
    url: 'https://example.com/GeoLite2-City.tar.gz'
```

### Target download path

[](#target-download-path)

By default, new databases downloaded in `%kernel.cache_dir%/{edition_id}.mmdb`, where `edition_id` is a character ID name from first column on [download page](https://www.maxmind.com/en/download_files). That is, by default, the new database will be downloaded into folder `var/cache/{env}/`. Keeping the database in the cache folder for each environment may not be optimal. You can choose a common directory for all environments.

```
huluti_geoip:
    license: 'XXXXXXXXXXXXXXXX'
    edition: 'GeoLite2-City'
    path: '%kernel.project_dir%/var/GeoLite2-City.mmdb'
```

#### Localization

[](#localization)

By default, the English locale is used for GeoIP record. You can change the locale for record and declare multiple locales for fallback.

```
huluti_geoip:
    license: 'XXXXXXXXXXXXXXXX'
    edition: 'GeoLite2-City'
    locales: [ 'ru', 'en' ]
```

Usage
-----

[](#usage)

You can get GeoIP2 reader service:

```
use GeoIp2\Database\Reader;

// get a GeoIP2 reader
$reader = $this->get(Reader::class);
// or
//$reader = $this->get('geoip2.reader');

// get a GeoIP2 City model
$record = $reader->city('128.101.101.101');

print($record->country->isoCode . "\n"); // 'US'
print($record->country->name . "\n"); // 'United States'
print($record->country->names['zh-CN'] . "\n"); // '美国'

print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'

print($record->city->name . "\n"); // 'Minneapolis'

print($record->postal->code . "\n"); // '55455'

print($record->location->latitude . "\n"); // 44.9733
print($record->location->longitude . "\n"); // -93.2323
```

For more example see the [GeoIP2](https://github.com/maxmind/GeoIP2-php) library.

Multiple databases
------------------

[](#multiple-databases)

You can use multiple GeoIP databases in one application. Need update configuration file.

```
huluti_geoip:
    databases:
        default:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-City'
        country:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-Country'
        asn:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-ASN'
```

Using in application:

```
// get a GeoIP2 reader for City database
$default_reader = $this->get('geoip2.database.default_reader');
// or
//$default_reader = $this->get(Reader::class);
// or
//$default_reader = $this->get('geoip2.reader');

// get a GeoIP2 reader for Country database
$country_reader = $this->get('geoip2.database.country_reader');

// get a GeoIP2 reader for ASN database
$asn_reader = $this->get('geoip2.database.asn_reader');
```

You can rename the default database.

```
huluti_geoip:
    default_database: 'city'
    databases:
        asn:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-ASN'
        city:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-City'
        country:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-Country'
```

```
// get a GeoIP2 reader for City database
$default_reader = $this->get('geoip2.database.city_reader');
// or
//$default_reader = $this->get(Reader::class);
// or
//$default_reader = $this->get('geoip2.reader');
```

In order not to repeat the license key and locales for each database, you can specify them once.

```
huluti_geoip:
    license: 'XXXXXXXXXXXXXXXX' # global license
    locales: [ 'ru', 'en' ] # global locales
    default_database: 'city'
    databases:
        asn:
            edition: 'GeoLite2-ASN'
            locales: [ 'fr' ] # customize locales
        city:
            edition: 'GeoLite2-City'
            url: 'https://example.com/GeoLite2-City.tar.gz' # customize url
            path: '%kernel.project_dir%/var/GeoLite2-City.mmdb' # customize path
        country:
            edition: 'GeoLite2-Country'
            license: 'YYYYYYYYYYYYYYYY' # customize license
```

### GeoIP data in client locale

[](#geoip-data-in-client-locale)

If you want to show the GeoIP data to the user and show them in the user locale, then you can use the reader factory.

```
use Huluti\Bundle\GeoIP2Bundle\Reader\ReaderFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class GeoIPController
{
    public function index(Request $request, ReaderFactory $factory): Response
    {
        $client_locale = $request->getLocale();
        $client_ip = $request->getClientIp();
        $database_name = 'default';
        $fallback_locale = 'en';

        $reader = $factory->create($database_name, [$client_locale, $fallback_locale]);
        $record = $reader->city($client_ip);

        return new Response(sprintf('You are from %s?', $record->country->name));
    }
}
```

Console commands
----------------

[](#console-commands)

### Update GeoIP database

[](#update-geoip-database)

Execute console command for update all databases:

```
php bin/console geoip2:update

```

If you use multiple databases, then for config:

```
huluti_geoip:
    # ...
    databases:
        asn:
            # ...
        city:
            # ...
        country:
            # ...
```

You can update several databases:

```
php bin/console geoip2:update city country

```

Optionally installing splitbrain/php-archive uses significantly less memory when updating a database and can avoid out of memory errors:

```
composer req splitbrain/php-archive

```

### Download GeoIP database

[](#download-geoip-database)

You can download custom database with console command:

```
php bin/console geoip2:download https://example.com/GeoLite2-City.tar.gz /path/to/GeoLite2-City.mmdb

```

License
-------

[](#license)

This bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE

###  Health Score

42

—

FairBetter than 89% of packages

Maintenance79

Regular maintenance activity

Popularity25

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 86.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

Unknown

Total

1

Last Release

113d ago

### Community

Maintainers

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

---

Top Contributors

[![peter-gribanov](https://avatars.githubusercontent.com/u/1954436?v=4)](https://github.com/peter-gribanov "peter-gribanov (287 commits)")[![Huluti](https://avatars.githubusercontent.com/u/5782198?v=4)](https://github.com/Huluti "Huluti (19 commits)")[![Lustmored](https://avatars.githubusercontent.com/u/2358046?v=4)](https://github.com/Lustmored "Lustmored (11 commits)")[![mreiden](https://avatars.githubusercontent.com/u/6321246?v=4)](https://github.com/mreiden "mreiden (5 commits)")[![Bukashk0zzz](https://avatars.githubusercontent.com/u/1908342?v=4)](https://github.com/Bukashk0zzz "Bukashk0zzz (3 commits)")[![antedebaas](https://avatars.githubusercontent.com/u/5467398?v=4)](https://github.com/antedebaas "antedebaas (3 commits)")[![jaimenj](https://avatars.githubusercontent.com/u/6050201?v=4)](https://github.com/jaimenj "jaimenj (1 commits)")[![classaxe](https://avatars.githubusercontent.com/u/9293057?v=4)](https://github.com/classaxe "classaxe (1 commits)")[![ChrisKinnan](https://avatars.githubusercontent.com/u/53341594?v=4)](https://github.com/ChrisKinnan "ChrisKinnan (1 commits)")[![aalwash](https://avatars.githubusercontent.com/u/3220347?v=4)](https://github.com/aalwash "aalwash (1 commits)")

---

Tags

symfonysymfony-bundle

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/huluti-geoip2-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/huluti-geoip2-bundle/health.svg)](https://phpackages.com/packages/huluti-geoip2-bundle)
```

PHPackages © 2026

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