Installation:
composer require cimus/geo-bundle
(Note: Fix typo in package name: geo-bundle instead of geo-budle.)
Enable the Bundle:
Add to config/bundles.php:
Cimus\GeoBundle\CimusGeoBundle::class => ['all' => true],
Initialize Database: Run the update command to download and convert the IP database:
php artisan cimus:geo:update
(Assuming Laravel compatibility layer is implemented; adjust if using Symfony CLI.)
First Usage: Inject the service and query an IP:
$geoData = app('cimus.geo')->search('8.8.8.8');
// Returns array with country, region, city, etc.
Detect User Location in a Controller:
use Illuminate\Http\Request;
public function showLocation(Request $request) {
$ip = $request->ip();
$geo = app('cimus.geo')->search($ip);
return view('location', ['geo' => $geo]);
}
IP Lookup in Middleware: Attach middleware to log or redirect based on geolocation:
namespace App\Http\Middleware;
use Closure;
class GeoMiddleware {
public function handle($request, Closure $next) {
$geo = app('cimus.geo')->search($request->ip());
$request->merge(['geo' => $geo]);
return $next($request);
}
}
Caching Responses: Cache results for 24 hours to reduce database hits:
$geo = Cache::remember("geo_{$ip}", 86400, function() use ($ip) {
return app('cimus.geo')->search($ip);
});
Bulk Lookup: Process a list of IPs (e.g., for analytics):
$ips = ['1.1.1.1', '2.2.2.2'];
$results = array_map(fn($ip) => app('cimus.geo')->search($ip), $ips);
Laravel Service Provider:
Bind the Symfony service to Laravel’s container in AppServiceProvider:
$this->app->singleton('cimus.geo', function($app) {
return $app->make('cimus.geo');
});
Configuration:
Override default settings (e.g., database path) in config/services.php:
'geo' => [
'database_path' => storage_path('app/geo_db'),
],
Event-Driven Updates: Trigger updates via Laravel’s scheduler:
// app/Console/Kernel.php
protected function schedule(Schedule $schedule) {
$schedule->command('cimus:geo:update')->weekly();
}
Database Initialization:
cimus:geo:update fails if the directory lacks write permissions.storage/app/geo_db is writable:
chmod -R 755 storage/app/geo_db
IP Format Sensitivity:
256.1.1.1) may crash the lookup.if (!filter_var($ip, FILTER_VALIDATE_IP)) {
throw new \InvalidArgumentException("Invalid IP: {$ip}");
}
Outdated Data:
php artisan cimus:geo:update >> storage/logs/geo_update.log 2>&1
Empty Results:
storage/app/geo_db/ipgeobase.dat exists).E_WARNING during binary file reads).Performance:
// In AppServiceProvider::boot()
app('cimus.geo')->search('0.0.0.0'); // Force initialization
Custom Data Fields:
GeoService (if source code is available).$geo = app('cimus.geo')->search($ip);
$geo['custom_field'] = $this->getCustomData($ip);
Alternative Data Sources:
$this->app->bind('cimus.geo', function() {
return new CustomGeoService();
});
Laravel Scout Integration:
$results = Model::where('country', $geo['country'])->get();
How can I help you explore Laravel packages today?