|
6 | 6 | Determine the geographical location and currency of website visitors based on their IP addresses. |
7 | 7 |
|
8 | 8 |
|
| 9 | +## About this fork |
| 10 | + |
| 11 | +We have forked [Torann/laravel-geoip](https://github.com/Torann/laravel-geoip) as it’s almost not maintained anymore. |
| 12 | +This fork works with modern PHP versions only (8.0+). |
| 13 | +Also, as for any InteractionDesignFoundation project, we are going to improving code quality by using types, static analysers, tests and linters. |
| 14 | +But don’t worry, we are using SemVer 2.0. |
| 15 | + |
| 16 | + |
9 | 17 | ## Installation |
10 | 18 |
|
11 | | -You can install the package in to a Laravel app that uses [Nova](https://nova.laravel.com) via composer: |
| 19 | +From the command line run: |
12 | 20 |
|
13 | | -```bash |
| 21 | +```sh |
14 | 22 | composer require interaction-design-foundation/laravel-geoip |
15 | 23 | ``` |
16 | 24 |
|
| 25 | +This package also comes with an optional facade, which provides an easy way to call the class. |
| 26 | +Open up `config/app.php` and find the aliases key. |
| 27 | + |
| 28 | +```php |
| 29 | +'aliases' => [ |
| 30 | + 'GeoIP' => \InteractionDesignFoundation\GeoIP\Facades\GeoIP::class, |
| 31 | +]; |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +### Publish the configurations |
| 36 | + |
| 37 | +Run this on the command line from the root of your project: |
| 38 | +```sh |
| 39 | +php artisan vendor:publish --provider="InteractionDesignFoundation\GeoIP\GeoIPServiceProvider" --tag=config |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | +## Configuration |
| 44 | + |
| 45 | +Quick breakdown of the two main options in the configuration file. |
| 46 | +To find out more simple open the `config/geoip.php` file. |
| 47 | + |
| 48 | + |
| 49 | +### Service Configuration |
| 50 | + |
| 51 | +To simplify and keep things clean, all third party composer packages, that are needed for a service, are installed separately. |
| 52 | + |
| 53 | +For further configuration options checkout the services page. |
17 | 54 |
|
18 | | -## Official Documentation |
19 | 55 |
|
20 | | - @todo |
| 56 | +### Caching Configuration |
21 | 57 |
|
| 58 | +GeoIP uses Laravel’s default caching to store queried IP locations. |
| 59 | +This is done to reduce the number of calls made to the selected service, as some of them are rate limited. |
| 60 | + |
| 61 | +Options: |
| 62 | + - `all` all location are cached |
| 63 | + - `some` cache only the requesting user |
| 64 | + - `none` caching is completely disable |
| 65 | + |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +There are few options to use the package: |
| 70 | + - `geoip()` helper function |
| 71 | + - `InteractionDesignFoundation\GeoIP\Facades\GeoIP` facade |
| 72 | + |
| 73 | +```php |
| 74 | +geoip()->getLocation('27.974.399.65'); // Get the location from the provided IP. |
| 75 | +geoip()->getClientIP(); // Will return the user IP address. |
| 76 | +``` |
| 77 | + |
| 78 | +Example of Location object: |
| 79 | +```php |
| 80 | +\InteractionDesignFoundation\GeoIP\Location { |
| 81 | +[ |
| 82 | + 'ip' => '1.1.1.1', |
| 83 | + 'iso_code' => 'US', |
| 84 | + 'country' => 'United States', |
| 85 | + 'city' => 'New Haven', |
| 86 | + 'state' => 'CT', |
| 87 | + 'state_name' => 'Connecticut', |
| 88 | + 'postal_code' => '06510', |
| 89 | + 'lat' => 41.28, |
| 90 | + 'lon' => -72.88, |
| 91 | + 'timezone' => 'America/New_York', |
| 92 | + 'continent' => 'NA', |
| 93 | + 'currency' => 'USD', |
| 94 | + 'default' => false, |
| 95 | + ] |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +`Location` class implements `\ArrayAccess` interface, means you can access properties of the `Location` object using array access syntax: |
| 100 | +```php |
| 101 | +$location = geoip()->getLocation(); |
| 102 | +$city = $location['city']; |
| 103 | +``` |
| 104 | + |
| 105 | +### Artisan |
| 106 | + |
| 107 | +Some services require downloading and use local database to detect Location by IP address. |
| 108 | +There is a console command to download/update database: |
| 109 | +```sh |
| 110 | +php artisan geoip:update |
| 111 | +``` |
| 112 | + |
| 113 | +Some cache drivers offer the ability to clear cached locations: |
| 114 | +```sh |
| 115 | +php artisan geoip:clear |
| 116 | +``` |
22 | 117 |
|
23 | 118 | ## Contributions |
24 | 119 |
|
|
0 commit comments