Install the Package
composer require avtonom/geocoder-bundle ~1.1
Ensure your composer.json has PHP ≥5.3.2 and Symfony 2.3/3.x.
Register the Bundle
Add to app/AppKernel.php (Symfony 2.x) or config/bundles.php (Symfony 3.x+):
new Avtonom\GeocoderBundle\AvtonomGeocoderBundle(),
new YamilovS\SypexGeoBundle\YamilovsSypexGeoBundle(),
new Bazinga\Bundle\GeocoderBundle\BazingaGeocoderBundle(),
Configure the Bundle
Define the database path in config/packages/yamilovs_sypex_geo.yaml (Symfony 3.x+) or app/config/config.yml:
yamilovs_sypex_geo:
database_path: "%kernel.project_dir%/var/SypexGeoDatabase/SxGeoCity.dat"
Update the Geo Database Run the console command to fetch the latest database:
php bin/console yamilovs:sypex-geo:update-database-file
Alternative: Manually download/unzip from SypexGeo.
First Use Case: Geocoding an IP
Inject the geocoder service and use the sypex_geo provider:
use Bazinga\Bundle\GeocoderBundle\Geocoder\GeocoderManagerInterface;
class MyService {
public function __construct(private GeocoderManagerInterface $geocoder) {}
public function getLocation(string $ip): ?array {
$result = $this->geocoder->geocodeQuery('sypex_geo', $ip);
return $result->first()?->getCoordinates();
}
}
Geocoding IP Addresses
Use the sypex_geo provider for fast, offline IP-to-location lookups:
$geocoder = $this->geocoder->getProvider('sypex_geo');
$result = $geocoder->geocode('192.0.2.1');
$city = $result->first()?->getCity();
Chaining Providers
Combine sypex_geo with other providers (e.g., Google Maps) for fallback logic:
# config/packages/bazinga_geocoder.yaml
bazinga_geocoder:
providers:
chain:
providers: [avtonom_geocoder.geocoder.sypex_geo, google_maps]
Reverse Geocoding Convert coordinates to human-readable addresses:
$reverse = $geocoder->reverse('51.5074', '-0.1278');
$address = $reverse->first()?->getStreet();
GeocoderType for address fields:
use Bazinga\Bundle\GeocoderBundle\Form\Type\GeocoderType;
$builder->add('address', GeocoderType::class, [
'provider' => 'sypex_geo',
'error_bubbling' => true,
]);
use Bazinga\Bundle\GeocoderBundle\Event\GeocodeEvent;
public function onGeocodeFailure(GeocodeEvent $event) {
if ($event->getProviderName() === 'sypex_geo') {
$this->logger->error('SypexGeo failed for IP: '.$event->getQuery());
}
}
$cache = $this->container->get('cache.app');
$key = 'geo_'.$ip;
if (!$cache->has($key)) {
$result = $geocoder->geocode($ip);
$cache->set($key, $result, 3600); // Cache for 1 hour
}
Database Path Issues
SxGeoCity.dat path is absolute and writable.%kernel.project_dir% (Symfony 2.x) or %kernel.project_dir% (Symfony 3.x+) in config.php bin/console debug:config yamilovs_sypex_geo
Outdated Database
SxGeoCity.dat) may be stale. Always run:
php bin/console yamilovs:sypex-geo:update-database-file
var/SypexGeoDatabase/ with the latest from SypexGeo.Provider Name Mismatch
avtonom_geocoder.geocoder.sypex_geo (not sypex_geo directly).providers:
chain:
providers: [avtonom_geocoder.geocoder.sypex_geo]
IPv6 Limitations
google_maps).Symfony 4/5 Compatibility
geocoder-php/geocoder with the SxGeo adapter directly.$result = $geocoder->geocode('192.0.2.1');
dump($result->getMessage()); // Debug errors
"192.0.2.1"), not objects or malformed strings.SxGeo object:
$provider = $this->geocoder->getProvider('avtonom_geocoder.geocoder.sypex_geo');
dump($provider->getHandler()->getGeo());
Custom Database Paths Override the default path via environment variables:
yamilovs_sypex_geo:
database_path: "%env(SYPEX_GEO_PATH)%"
Set in .env:
SYPEX_GEO_PATH=/custom/path/SxGeoCity.dat
Extending Geocoder Results
Add custom methods to the result object by subclassing Geocoder\Result\Result:
class ExtendedResult extends \Geocoder\Result\Result {
public function getCountryCode(): ?string {
return $this->getData()['country_code'] ?? null;
}
}
Bind it in services.yaml:
services:
App\Geocoder\ExtendedResult:
tags: ['bazinga.geocoder.result']
Bulk Geocoding Process multiple IPs efficiently:
$ips = ['192.0.2.1', '198.51.100.2'];
$results = $geocoder->geocodeBatch($ips);
foreach ($results as $ip => $result) {
$this->logger->info(sprintf('IP %s -> %s', $ip, $result->first()?->getCity()));
}
How can I help you explore Laravel packages today?