ndevs-eu/geo-ip-bundle

Symfony bundle for Geo IP functionality

Installs: 91

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 0

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/ndevs-eu/geo-ip-bundle

v1.0.16 2025-08-11 07:19 UTC

This package is auto-updated.

Last update: 2025-09-11 07:34:07 UTC


README

PHPStan Level 9 CI Latest Stable Version Total Downloads License

🌍 NDevs GeoIP Bundle

Symfony bundle for detecting user geolocation based on IP address.
Supports MaxMind GeoLite2 and IP2Location, with optional fallback, mock IP in dev mode, and automatic request listener.

βš™οΈ Installation

  • composer require ndevs-eu/geo-ip-bundle

  • Copy example config to /config/packages/geo_ip.yaml

  • Add env variables to your .env file (GEOIP_MAXMIND_LICENSE_KEY, GEOIP_IP2LOCATION_LICENSE_KEY)

  • Run bin/console geoip:pull-maxmind-database

  • Run bin/console geoip:pull-ip2loc-lite-database

πŸ“ Configuration structure (config/packages/geo_ip.yaml)

geo_ip:
    listener_enabled: false # Enables request listener to attach geo data

    resolver:
        primary: maxmind       # or 'ip2location'
        fallback: ip2location  # optional

    maxmind:
        path: '%kernel.project_dir%/var/geoip-maxmind/'
        key: '%env(GEOIP_MAXMIND_LICENSE_KEY)%'

    ip2location:
        path: '%kernel.project_dir%/var/geoip-ip2loc/'
        key: '%env(GEOIP_IP2LOCATION_LICENSE_KEY)%'

In dev environment you can add a mock IP for testing:

when@dev:
    geo_ip:
        mock_ip: '185.170.167.18'

πŸ§ͺ Usage in code

βœ… Accessing geo data

$geoData = $request->attributes->get('geoIp');

if ($geoData) {
    $country = $geoData['country'];
    $city = $geoData['city'];
}

The listener injects geoIp attribute into each request when enabled.

🌍 Downloading databases

πŸ“₯ MaxMind (GeoLite2 City)

  1. Register at maxmind.com
  2. Get your License Key
  3. Download database manually or run:
php bin/console geoip:pull-maxmind-database

πŸ“₯ IP2Location (Lite BIN)

  1. Get free version
  2. Download manually or run:
php bin/console geoip:pull-ip2loc-lite-database

Make sure the path in your config matches the location of extracted files.

πŸ§ͺ Mocking IP in dev

To test without relying on real headers:

when@dev:
    geo_ip:
        mock_ip: '8.8.8.8'

🧰 Available Console Commands

Command Description
geoip:pull-maxmind-database Downloads the latest MaxMind GeoIP DB
geoip:pull-ip2loc-lite-database Downloads the latest IP2Location Lite DB

βœ… Requirements

  • PHP 8.1+
  • Symfony 6.3+
  • Extensions:
    • ext-json
    • ext-mbstring
    • ext-zip
    • ext-curl
  • Composer
  • MaxMind GeoIP2 (City or Country)

πŸ“Œ Features

βœ… Auto-detects IP from headers (X-Forwarded-For, Cloudflare, etc.)
βœ… Supports fallback resolver if primary fails
βœ… Allows IP mocking in dev mode
βœ… Docker/proxy/CDN-friendly
βœ… Easy integration via DI

πŸ™‹ FAQ

What if the IP is not found?

geoIp attribute will contain null values. No exception is thrown.

What if I don’t use IP2Location?

Just don't define a fallback resolver in config.

πŸ“„ License

MIT

GitAds Sponsored

Sponsored by GitAds