Bundle for use maxmind/GeoIP2 in Symfony.
Pretty simple with Composer, run:
composer require gpslab/geoip2Add GpsLabGeoIP2Bundle to your application kernel
Symfony <3.4
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new GpsLab\Bundle\GeoIP2Bundle\GpsLabGeoIP2Bundle(),
);
}Symfony >4.0
// config/bundles.php
return [
// ...
GpsLab\Bundle\GeoIP2Bundle\GpsLabGeoIP2Bundle::class => ['all' => true],
];Default configuration:
gpslab_geoip:
# Path to download GeoIP database.
# It's a default value. You can change it.
cache: '%kernel.cache_dir%/GeoLite2-City.mmdb'
# URL for download new GeoIP database.
# It's a default value. You can change it.
url: 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz'
# Get model data in this locale
# It's a default value. You can change it.
locales: [ '%locale%' ]You can get GeoIP2 reader service:
// get a GeoIP2 City model
$record = $this->get('geoip2.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.2323For more example see the GeoIP2 library.
Execute command for update database:
Symfony <2.8
php app/console geoip2:update
Symfony >3.0
php bin/console geoip2:update
Add to your composer.json event callbacks in a scripts section:
Symfony <3.0
{
"scripts": {
"post-install-cmd": [
"GpsLab\\Bundle\\GeoIP2Bundle\\Composer\\ScriptHandler::updateDatabase"
],
"post-update-cmd": [
"GpsLab\\Bundle\\GeoIP2Bundle\\Composer\\ScriptHandler::updateDatabase"
]
}
}Symfony >3.1
{
"scripts": {
"symfony-scripts": [
"GpsLab\\Bundle\\GeoIP2Bundle\\Composer\\ScriptHandler::updateDatabase"
]
}
}This bundle is under the MIT license. See the complete license in the file: LICENSE