ndevs-eu/geo-ip-bundle
最新稳定版本:v1.0.16
Composer 安装命令:
composer require ndevs-eu/geo-ip-bundle
包简介
Symfony bundle for Geo IP functionality
README 文档
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
devenvironment 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
geoIpattribute 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-jsonext-mbstringext-zipext-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
统计信息
- 总下载量: 108
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-08