- PHP 100%
|
|
||
|---|---|---|
| src | ||
| .gitattributes | ||
| .gitignore | ||
| composer.json | ||
| LICENSE.md | ||
| README.md | ||
Laravel GeoIP
Thin Laravel wrapper around the free geoPlugin IP geolocation service. Results are cached by IP-hash so repeated lookups for the same address don't re-hit the upstream API. Tested on PHP 8.2+ and Laravel 11/12.
Installation
composer require lithiumhosting/laravel-geoip
The service provider auto-registers. To use the facade with a shorter
alias, register it in bootstrap/app.php or in config/app.php:
'aliases' => [
'LaraGeo' => \LithiumHosting\LaraGeo\LaraGeoFacade::class,
],
Usage
use LithiumHosting\LaraGeo\LaraGeoFacade as LaraGeo;
use LithiumHosting\LaraGeo\LaraGeoException;
try {
$info = LaraGeo::getInfo('8.8.8.8');
// $info is a stdClass with geoplugin_* properties
echo $info->geoplugin_city.', '.$info->geoplugin_countryName;
} catch (LaraGeoException $e) {
// bad IP or upstream failure
}
Calling getInfo() without an argument falls through to the current
request's client IP.
Trait usage
When you want to derive a "City, Region, Country" string from any class that owns an IP, add the trait:
use LithiumHosting\LaraGeo\Traits\GeoIp;
class Visitor
{
use GeoIp;
}
$location = (new Visitor)->parseIp('8.8.8.8')->ipToCityStateCountry();
// "Mountain View, California, US"
The trait swallows upstream failures and falls back to a blank result so your render path doesn't blow up when the API is unreachable.
License
This package, laravel-geoip is licensed under The MIT License (MIT). Please see License File for more information.
Is it any good?
Yes.
When people first hear about a new product, they frequently ask if it is any good. A Hacker News user remarked:
Note to self: Starting immediately, all raganwald projects will have a "Is it any good?" section in the readme, and the answer shall be "yes.".