middlewares/geolocation

中间件,通过IP地址定位客户端

v3.0.0 2020-12-04 00:07 UTC

This package is auto-updated.

Last update: 2024-08-29 05:41:46 UTC


README

Packagist上的最新版本 软件许可 测试 总下载量 ![SensioLabs Insight][ico-sensiolabs]

通过IP地址和Geocoder定位客户端,并将结果保存为请求属性。

要求

安装

此包可通过Composer安装和自动加载,作为middlewares/geolocation

composer require middlewares/geolocation

示例

$freeGeoIpProvider = new Geocoder\Provider\FreeGeoIp($adapter);

$dispatcher = new Dispatcher([
    new Middlewares\Geolocation($freeGeoIpProvider),

    function ($request) {
        //Get the client location
        $location = $request->getAttribute('client-location');

        $country = $location->first()->getCountry();
    }
]);

$response = $dispatcher->dispatch(new ServerRequest());

选项

__construct(Geocoder\Provider\Provider $provider)

用于定位客户端的geocoder提供者。

建议将其配置为缓存响应。

ipAttribute(string $ipAttribute)

默认使用REMOTE_ADDR服务器参数获取客户端IP。此选项允许使用请求属性。与IP检测中间件结合使用很有用,例如client-ip

attribute(string $attribute)

用于在服务器请求中存储客户端地址的属性名称。默认为client-location

有关最近更改的更多信息,请参阅CHANGELOG,有关贡献的详细信息,请参阅CONTRIBUTING

MIT许可(MIT)。有关更多信息,请参阅LICENSE