geolax/geolocate
Composer 安装命令:
composer require geolax/geolocate
包简介
Extensible geolocation lookup for Laravel — driver-based, addon-ready
README 文档
README
Extensible geolocation lookup for Laravel — driver-based, addon-ready.
Geolax Geolocate provides a clean, driver-based architecture for IP geolocation. Install the base package, pick an addon (or build your own), and start looking up IPs in seconds.
Installation
composer require geolax/geolocate
Publish the config file:
php artisan vendor:publish --tag="geolocate-config"
Configuration
// config/geolocate.php return [ 'default' => env('GEOLOCATE_DRIVER', 'freeipapi'), 'drivers' => [ 'freeipapi' => [ 'driver' => 'freeipapi', 'base_url' => env('GEOLOCATE_FREEIPAPI_URL', 'https://freeipapi.com'), 'api_key' => env('GEOLOCATE_FREEIPAPI_KEY'), 'server' => env('GEOLOCATE_FREEIPAPI_SERVER', 'free'), 'timeout' => 5, ], ], ];
Available Addons
| Addon | Driver Name | Package |
|---|---|---|
| FreeIPAPI | freeipapi |
geolax/freeipapi |
Install an addon alongside the base package. Addons are auto-discovered by Laravel — no manual registration needed.
Usage
Via Facade
use Geolax\Geolocate\Facades\Geolocate; $result = Geolocate::lookup('1.1.1.1'); $result->ipAddress; // "1.1.1.1" $result->countryName; // "Australia" $result->countryCode; // "AU" $result->cityName; // "Sydney" $result->latitude; // -33.8688 $result->longitude; // 151.209 $result->timezone; // "Australia/Sydney" $result->currency; // "AUD" $result->driver; // "freeipapi"
Via Dependency Injection
use Geolax\Geolocate\Contracts\Driver; class GeoController extends Controller { public function __construct(private Driver $geolocate) {} public function locate(Request $request) { $result = $this->geolocate->lookup($request->ip()); return response()->json($result->toArray()); } }
Lookup Current Request IP
// Pass null or no argument to look up the caller's IP $result = Geolocate::lookup();
Use a Specific Driver
$result = Geolocate::driver('freeipapi')->lookup('8.8.8.8');
Access Raw Provider Data
Each provider may return additional fields beyond the standard DTO. Access them via the raw property:
$result = Geolocate::lookup('1.1.1.1'); $result->raw['asn']; // "13335" $result->raw['asnOrganization']; // "Cloudflare, Inc." $result->raw['isProxy']; // false $result->raw['timeZones']; // ["Australia/Sydney", "Australia/Melbourne", ...]
The GeolocationResult DTO
Every lookup returns an immutable GeolocationResult with these properties:
| Property | Type | Description |
|---|---|---|
ipVersion |
?int |
4 or 6 |
ipAddress |
?string |
Resolved IP address |
latitude |
?float |
Geographic latitude |
longitude |
?float |
Geographic longitude |
countryName |
?string |
Full country name |
countryCode |
?string |
ISO 3166-1 alpha-2 |
regionName |
?string |
State/province/region |
regionCode |
?string |
Region code |
cityName |
?string |
City name |
zipCode |
?string |
Postal/ZIP code |
timezone |
?string |
Primary timezone |
continent |
?string |
Continent name |
continentCode |
?string |
Continent code |
currency |
?string |
Primary currency code |
driver |
?string |
Driver that produced this result |
raw |
array |
Full provider response |
Creating Your Own Addon
Any developer can create a geolocation addon without modifying the base package.
1. Create a Driver
<?php namespace Acme\IpInfo; use Geolax\Geolocate\Contracts\Driver; use Geolax\Geolocate\Data\GeolocationResult; class IpInfoDriver implements Driver { public function __construct(protected array $config = []) {} public function lookup(?string $ip = null): GeolocationResult { // Call your API, then map the response: return new GeolocationResult( ipAddress: $ip, countryName: 'United States', countryCode: 'US', driver: 'ipinfo', raw: $apiResponse, ); } }
2. Create a Service Provider
<?php namespace Acme\IpInfo; use Geolax\Geolocate\GeolocateManager; use Illuminate\Support\ServiceProvider; class IpInfoServiceProvider extends ServiceProvider { public function register(): void { $this->app->resolving(GeolocateManager::class, function (GeolocateManager $manager) { $manager->extend('ipinfo', function ($app, array $config) { return new IpInfoDriver($config); }); }); } }
3. Add Auto-Discovery
In your addon's composer.json:
{
"extra": {
"laravel": {
"providers": [
"Acme\\IpInfo\\IpInfoServiceProvider"
]
}
}
}
4. Users Configure It
// config/geolocate.php 'default' => 'ipinfo', 'drivers' => [ 'ipinfo' => [ 'driver' => 'ipinfo', 'api_key' => env('IPINFO_API_KEY'), ], ],
That's it. The base package handles everything else.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
geolax/geolocate 适用场景与选型建议
geolax/geolocate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「geolocation」 「laravel」 「geolocate」 「ip-lookup」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 geolax/geolocate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 geolax/geolocate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 geolax/geolocate 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Maps in minutes. Powered by the Google Maps API.
The official PHP library for apiip.net that allowing customers to automate IP address validation and geolocation lookup in websites, applications and back-office system. Visit our API docs at https://apiip.net/documentation
Supports GeoIP services (sypexgeo.net).
Alfabank REST API integration
Support for multiple GeoIP services.
Support for multiple GeoIP services.
统计信息
- 总下载量: 49
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-02