Install the Bundle
composer require alexeyfedotof/sypex-geo-bundle
Enable the bundle in config/bundles.php:
return [
// ...
AlexeyFedotof\SypexGeoBundle\SypexGeoBundle::class => ['all' => true],
];
Configure the Bundle
Edit config/packages/sypex_geo.yaml (auto-generated):
sypex_geo:
api_key: 'your_api_key_here' # Required for cloud API mode
cache_dir: '%kernel.cache_dir%/sypex_geo' # Cache directory
mode: 'file' # 'file' (local DB) or 'cloud' (API)
database_path: '%kernel.project_dir%/var/sypex_geo.db' # Only for 'file' mode
First Use Case: Fetch Geo Data Inject the service in a controller or command:
use AlexeyFedotof\SypexGeoBundle\Service\SypexGeoService;
public function __construct(private SypexGeoService $geoService) {}
public function getGeoData(Request $request): array
{
$ip = $request->getClientIp();
$data = $this->geoService->getGeoData($ip);
return $data; // e.g., ['country' => 'US', 'city' => 'New York']
}
Local Database Mode (Offline)
php bin/console sypex-geo:update-database
Cloud API Mode (Real-Time)
mode: 'cloud' and provide an API key.$this->geoService->getGeoData($ip, ['use_cache' => false]); // Bypass cache
Event-Driven Geo Enrichment
sypex_geo.data_fetched event to extend data:
// src/EventListener/SypexGeoListener.php
public function onGeoDataFetched(GeoDataEvent $event) {
$event->setData(['custom_field' => 'value']);
}
services.yaml:
services:
App\EventListener\SypexGeoListener:
tags:
- { name: 'kernel.event_listener', event: 'sypex_geo.data_fetched' }
Bulk IP Lookup
$ips = ['1.2.3.4', '5.6.7.8'];
$results = $this->geoService->getGeoDataBatch($ips);
# config/packages/cache.yaml
frameworks:
cache:
app: cache.adapter.redis
$this->geoService->setRateLimiter(new CustomRateLimiter());
$this->geoService = $this->createMock(SypexGeoService::class);
$this->geoService->method('getGeoData')->willReturn(['country' => 'Test']);
Database Updates
sypex-geo:update-database) leads to stale data.API Key Leaks
api_key in config/packages/sypex_geo.yaml exposes it in Git.%env(resolve:...)% or env():
api_key: '%env(SYPEX_GEO_API_KEY)%'
Cache Invalidation
php bin/console cache:clear
IPv6 Support
yamilovs/sypex-geo) has limited IPv6 support. Test thoroughly if your audience uses IPv6.Cloud Mode Latency
Enable Verbose Logging
Add to config/packages/sypex_geo.yaml:
debug: true
Logs appear in var/log/dev.log.
Validate IP Input
The service silently returns null for invalid IPs. Add validation:
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
throw new \InvalidArgumentException('Invalid IP address');
}
Custom Data Fields
Extend the GeoData class to add fields:
// src/Entity/CustomGeoData.php
class CustomGeoData extends GeoData {
public function getCustomField() { ... }
}
Override the service to return your class:
services:
App\Service\CustomSypexGeoService:
decorates: 'alexeyfedotof.sypex_geo.service'
arguments: ['@alexeyfedotof.sypex_geo.service']
Database Path Overrides Dynamically set the DB path based on environment:
$this->geoService->setDatabasePath(
$this->getParameter('kernel.project_dir') . '/var/sypex_geo_prod.db'
);
Fallback Logic Implement a fallback for API/cloud failures:
try {
return $this->geoService->getGeoData($ip);
} catch (\RuntimeException $e) {
return $this->fallbackGeoService->getData($ip);
}
getGeoDataBatch() instead of looping.$cache->set('geo_data_' . $ip, $data, ['sypex_geo_db'], 86400);
How can I help you explore Laravel packages today?