stevebauman/location
Retrieve a user’s geolocation from their IP in Laravel. Provides a simple Location facade to get city, region, country, coordinates, timezone and more, with multiple driver support (e.g., IP2Location, IP-API, MaxMind) plus caching and testing helpers.
Location::get()), service providers, and middleware. This ensures minimal boilerplate and tight integration with Laravel’s request lifecycle (e.g., resolving user location on every request).Macroable trait enables custom methods (e.g., Location::macro('getContinent', ...)) or drivers without forking the package. This is critical for domain-specific extensions (e.g., adding "region" logic for a US-centric app).Location::fake()) simplifies CI/CD and edge-case testing (e.g., simulating VPNs or Tor exits).stevebauman/location), with minimal configuration (.env keys). The package auto-handles database downloads (MaxMind) and API key management.// app/Http/Middleware/SetGeoData.php
public function handle($request, Closure $next) {
$request->merge(['geo' => Location::get()]);
return $next($request);
}
LOCATION_CACHE_TTL=3600 // Cache for 1 hour
config/location.php) mitigate single points of failure..mmdb files (~5MB) may require storage permissions or CDN hosting for large-scale apps.storage/framework/cache/location for errors.ipapi.co) lack IPv6 support. Validate coverage for target regions (e.g., Asia-Pacific).Location::shouldFail();
LOCATION_CACHE_TTL=86400).Location::fake() with custom IP ranges.fileinfo (MaxMind) and curl/guzzle (API drivers). Most PHP stacks (e.g., Laravel Forge, Heroku) include these by default..mmdb files (no RDBMS). API drivers are stateless.// app/Providers/AppServiceProvider.php
public function boot() {
Location::extend('custom', function () {
return new CustomDriver();
});
}
Assessment Phase:
Pilot Integration:
.env with the primary driver (e.g., MaxMind):
LOCATION_DRIVER=maxmind
MAXMIND_LICENSE_KEY=your_license_key
LOCATION_CACHE_DRIVER=redis
LOCATION_CACHE_TTL=3600
$position = Location::get();
$country = $position->countryCode; // 'US'
// app/Http/Middleware/ResolveGeoData.php
public function handle($request, Closure $next) {
$request->merge(['geo' => Location::get()]);
return $next($request);
}
Location::fake([
'ip' => '8.8.8.8',
'country_code' => 'US',
'city' => 'Mountain View',
]);
Advanced Integration:
Location::extend('acme', function () {
return new AcmeDriver(config('services.acme.api_key'));
});
Location::macro('isEu', function () {
return in_array($this->countryCode, ['DE', 'FR', 'IT', 'ES', 'etc.']);
});
config/location.php:
'fallbacks' => [
'maxmind' => ['ipinfo'],
'ipinfo' => ['ip2location'],
],
Production Rollout:
Location::get(); // Log metrics via Laravel Telescope or Prometheus.
LOCATION_CACHE_TTL based on use case (e.g., 1 hour for analytics, 5 minutes for real-time).How can I help you explore Laravel packages today?