Package Data | |
---|---|
Maintainer Username: | stevebauman |
Maintainer Contact: | steven_bauman@outlook.com (Steve Bauman) |
Package Create Date: | 2014-05-04 |
Package Last Update: | 2024-12-08 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-22 03:08:10 |
Package Statistics | |
---|---|
Total Downloads: | 4,724,105 |
Monthly Downloads: | 145,564 |
Daily Downloads: | 1,399 |
Total Stars: | 1,142 |
Total Watchers: | 32 |
Total Forks: | 181 |
Total Open Issues: | 7 |
Retrieve a users location from their IP address using external web services, or through a flat-file database hosted on your server.
Run the following command in the root of your project:
composer require stevebauman/location
Note: If you're using Laravel 5.5, you can skip the registration of the service provider and the facade, as they are registered automatically.
Add the service provider in config/app.php
:
Stevebauman\Location\LocationServiceProvider::class,
Add the alias in your config/app.php
file:
'Location' => Stevebauman\Location\Facades\Location::class,
Publish the config file:
php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"
$position = Location::get();
// Returns instance of Stevebauman\Location\Position
$position = Location::get('192.168.1.1');
Available drivers at the moment are:
To setup MaxMind to retrieve the users location from your own server, download the database file here:
http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz
maxmind
folder inside your database
directory (database/maxmind
)maxmind
directoryYou should end up with a folder path of: my-laravel-app/database/maxmind/GeoLite2-City.mmdb
.
Then, set your default driver to Stevebauman\Location\Drivers\MaxMind::class
, and you're all set!
Note: Keep in mind, you'll need to update this file continuously to retrieve the most current information.
In the config file, you can specify as many fallback drivers as you wish. It's recommended to install
the MaxMind database .mmdb
file so your always retrieving some generic location information from the user.
If an exception occurs trying to grab a driver (such as a 404 error if the providers API changes), it will automatically use the next driver in line.
To create your own driver, simply create a class in your application, and extend the abstract Driver:
namespace App\Location\Drivers;
use Illuminate\Support\Fluent;
use Stevebauman\Location\Position;
use Stevebauman\Location\Drivers\Driver;
class MyDriver extends Driver
{
public function url()
{
return '';
}
protected function hydrate(Position $position, Fluent $location)
{
$position->countryCode = $location->country_code;
return $position;
}
protected function process($ip)
{
try {
$response = json_decode(file_get_contents($this->url().$ip), true);
return new Fluent($response);
} catch (\Exception $e) {
return false;
}
}
}
Then, insert your driver class name into the configuration file:
/*
|--------------------------------------------------------------------------
| Driver
|--------------------------------------------------------------------------
|
| The default driver you would like to use for location retrieval.
|
*/
'driver' => App\Locations\MyDriver::class,