middlewares/geolocation

Middleware to geolocate the client using the ip address

Package info

github.com/middlewares/geolocation

pkg:composer/middlewares/geolocation

Statistics

Installs: 8 547

Dependents: 3

Suggesters: 0

Stars: 13

Open Issues: 0

v3.2.0 2025-08-05 12:09 UTC

Requires

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 27efb4164130ec95e6a4ed28eaa1dcece58cadce

httpgeolocationservermiddlewareIPgeocodepsr-7psr-15

This package is auto-updated.

Last update: 2026-06-05 14:07:54 UTC


README

👁 Latest Version on Packagist
👁 Software License
👁 Testing
👁 Total Downloads
![SensioLabs Insight][ico-sensiolabs]

Middleware to geolocate the client using the ip address and Geocoder and save the result as a request attribute.

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/geolocation.

composer require middlewares/geolocation

Example

$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());

Options

__construct(Geocoder\Provider\Provider $provider)

The geocoder provider used to geolocate the client.

It's also recommended to configure it to caching responses.

ipAttribute(string $ipAttribute)

By default uses the REMOTE_ADDR server parameter to get the client ip. This option allows to use a request attribute. Useful to combine with a ip detection middleware, for example client-ip.

attribute(string $attribute)

The attribute name used to store the client addresses in the server request. By default is client-location.

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.

The MIT License (MIT). Please see LICENSE for more information.