|
| 1 | +# middlewares/geolocation |
| 2 | + |
| 3 | +[![Latest Version on Packagist][ico-version]][link-packagist] |
| 4 | +[![Software License][ico-license]](LICENSE.md) |
| 5 | +[![Build Status][ico-travis]][link-travis] |
| 6 | +[![Quality Score][ico-scrutinizer]][link-scrutinizer] |
| 7 | +[![Total Downloads][ico-downloads]][link-downloads] |
| 8 | +[![SensioLabs Insight][ico-sensiolabs]][link-sensiolabs] |
| 9 | + |
| 10 | +Middleware to geolocate the client using the ip address and [Geocoder](https://github.com/geocoder-php/Geocoder) and save the result as a request attribute. |
| 11 | + |
| 12 | +**Note:** This middleware is intended for server side only |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +* PHP >= 5.6 |
| 17 | +* A [PSR-7](https://packagist.org/providers/psr/http-message-implementation) http mesage implementation ([Diactoros](https://github.com/zendframework/zend-diactoros), [Guzzle](https://github.com/guzzle/psr7), [Slim](https://github.com/slimphp/Slim), etc...) |
| 18 | +* A [PSR-15](https://github.com/http-interop/http-middleware) middleware dispatcher ([Middleman](https://github.com/mindplay-dk/middleman), etc...) |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +This package is installable and autoloadable via Composer as [middlewares/geolocation](https://packagist.org/packages/middlewares/geolocation). |
| 23 | + |
| 24 | +```sh |
| 25 | +composer require middlewares/geolocation |
| 26 | +``` |
| 27 | + |
| 28 | +## Example |
| 29 | + |
| 30 | +```php |
| 31 | +$dispatcher = new Dispatcher([ |
| 32 | + new Middlewares\Geolocation(), |
| 33 | + |
| 34 | + function ($request) { |
| 35 | + //Get the client location |
| 36 | + $location = $request->getAttribute('client-location'); |
| 37 | + |
| 38 | + $country = $location->first()->getCountry(); |
| 39 | + } |
| 40 | +]); |
| 41 | + |
| 42 | +$response = $dispatcher->dispatch(new ServerRequest()); |
| 43 | +``` |
| 44 | + |
| 45 | +## Options |
| 46 | + |
| 47 | +#### `__construct(Geocoder\Geocoder $geocoder = null)` |
| 48 | + |
| 49 | +The geocoder object used to geolocate the client. If it's not provided, use a generic `Geocoder\Provider\FreeGeoIp` instance. |
| 50 | + |
| 51 | +It's also recommended to configure it to [caching responses.](https://github.com/geocoder-php/Geocoder/blob/master/docs/cookbook/cache.md) |
| 52 | + |
| 53 | +#### `ipAttribute(string $ipAttribute)` |
| 54 | + |
| 55 | +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](https://github.com/middlewares/client-ip). |
| 56 | + |
| 57 | +#### `attribute(string $attribute)` |
| 58 | + |
| 59 | +The attribute name used to store the client addresses in the server request. By default is `client-location`. |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details. |
| 64 | + |
| 65 | +The MIT License (MIT). Please see [LICENSE](LICENSE) for more information. |
| 66 | + |
| 67 | +[ico-version]: https://img.shields.io/packagist/v/middlewares/geolocation.svg?style=flat-square |
| 68 | +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square |
| 69 | +[ico-travis]: https://img.shields.io/travis/middlewares/geolocation/master.svg?style=flat-square |
| 70 | +[ico-scrutinizer]: https://img.shields.io/scrutinizer/g/middlewares/geolocation.svg?style=flat-square |
| 71 | +[ico-downloads]: https://img.shields.io/packagist/dt/middlewares/geolocation.svg?style=flat-square |
| 72 | +[ico-sensiolabs]: https://img.shields.io/sensiolabs/i/36786f5a-2a15-4399-8817-8f24fcd8c0b4.svg?style=flat-square |
| 73 | + |
| 74 | +[link-packagist]: https://packagist.org/packages/middlewares/geolocation |
| 75 | +[link-travis]: https://travis-ci.org/middlewares/geolocation |
| 76 | +[link-scrutinizer]: https://scrutinizer-ci.com/g/middlewares/geolocation |
| 77 | +[link-downloads]: https://packagist.org/packages/middlewares/geolocation |
| 78 | +[link-sensiolabs]: https://insight.sensiolabs.com/projects/36786f5a-2a15-4399-8817-8f24fcd8c0b4 |
0 commit comments