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
Requires
- php: ^8.2 || ^8.3
- geoip2/geoip2: ^3.2
- ip2location/ip2location-php: ^9.7
- symfony/framework-bundle: ^6.4 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^10.0
README
π 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)
- Register at maxmind.com
- Get your
License Key
- Download database manually or run:
php bin/console geoip:pull-maxmind-database
π₯ IP2Location (Lite BIN)
- Get free version
- 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