Installation:
composer require likewinter/sypex-geo-laravel4
Add the service provider to config/app.php:
'providers' => [
// ...
'Likewinter\SypexGeo\SypexGeoServiceProvider',
],
Publish Config:
php artisan config:publish likewinter/sypex-geo-laravel4
Configure app/config/packages/likewinter/sypex-geo-laravel4.php with your SypexGeo API key and database path.
First Use Case: Fetch geolocation data for an IP address in a controller:
use Likewinter\SypexGeo\Facades\SypexGeo;
$ip = '8.8.8.8';
$geoData = SypexGeo::get($ip);
dd($geoData); // Returns array with country, city, coordinates, etc.
Geolocation Lookup:
// Single IP lookup
$data = SypexGeo::get('192.168.1.1');
// Batch lookup (if supported by the package)
$ips = ['1.1.1.1', '2.2.2.2'];
$results = SypexGeo::get($ips);
Middleware for Automatic Geo-IP: Create middleware to attach geo-data to requests:
namespace App\Http\Middleware;
use Closure;
use Likewinter\SypexGeo\Facades\SypexGeo;
class GeoIpMiddleware
{
public function handle($request, Closure $next)
{
$ip = $request->ip();
$request->merge(['geo' => SypexGeo::get($ip)]);
return $next($request);
}
}
Register in app/Http/Kernel.php:
protected $middleware = [
// ...
\App\Http\Middleware\GeoIpMiddleware::class,
];
Database Integration:
Store geo-data in a geo_ips table and cache results:
// Model: GeoIp.php
class GeoIp extends Model
{
protected $fillable = ['ip', 'country', 'city', 'lat', 'lng'];
public static function findOrCreateForIp($ip)
{
return static::firstOrCreate(
['ip' => $ip],
SypexGeo::get($ip)
);
}
}
API Response Enhancement: Attach geo-data to API responses:
return response()->json([
'data' => $user,
'geo' => request()->geo // From middleware
]);
Database File Path:
database_path in config matches the actual SypexGeo database file location (e.g., /path/to/sypexgeo.dat).storage_path('app') may not work; use an absolute path.IPv6 Support:
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$geoData = SypexGeo::get($ip);
}
Caching:
$cacheKey = "geo:{$ip}";
$geoData = Cache::remember($cacheKey, 86400, function() use ($ip) {
return SypexGeo::get($ip);
});
Rate Limiting:
Verify Database File: Check if the database file exists and is readable:
$path = config('sypex-geo.database_path');
if (!file_exists($path)) {
throw new \Exception("SypexGeo database not found at {$path}");
}
Log Missing IPs: Handle cases where an IP isn’t found in the database:
$geoData = SypexGeo::get($ip);
if (empty($geoData['country'])) {
Log::warning("No geo data for IP: {$ip}");
}
Custom Fields:
Extend the returned data by modifying the package’s get() method (if open-source) or wrapping it:
class ExtendedSypexGeo extends \Likewinter\SypexGeo\Facades\SypexGeo
{
public static function getWithEnrichment($ip)
{
$data = parent::get($ip);
$data['is_eu'] = self::isEuCountry($data['country']);
return $data;
}
private static function isEuCountry($countryCode)
{
// Logic to check EU membership
}
}
Fallback Mechanism: Implement a fallback to another geo-service if SypexGeo fails:
try {
$geoData = SypexGeo::get($ip);
} catch (\Exception $e) {
$geoData = GeoIp2::get($ip); // Alternative service
}
Testing: Mock the facade for unit tests:
$this->app->instance('sypex-geo', $mockGeoData);
How can I help you explore Laravel packages today?