All Projects → stevebauman → Location

stevebauman / Location

Licence: mit
Detect a users location by their IP Address.

Projects that are alternatives of or similar to Location

Laravel Location
A simple Laravel Package to sort Countries, States and Cities
Stars: ✭ 162 (-59.19%)
Mutual labels:  laravel, location
Ip Location Zh
获取 IP 地址的真实地理位置
Stars: ✭ 556 (+40.05%)
Mutual labels:  laravel, location
Locationpicker
A ready for use and fully customizable location picker for your app
Stars: ✭ 384 (-3.27%)
Mutual labels:  location
Zhihu App
laravel-vue-zhihu ✨
Stars: ✭ 395 (-0.5%)
Mutual labels:  laravel
I Educar
Lançando o maior software livre de educação do Brasil!
Stars: ✭ 388 (-2.27%)
Mutual labels:  laravel
Crater
Open Source Invoicing Solution for Individuals & Businesses
Stars: ✭ 4,897 (+1133.5%)
Mutual labels:  laravel
Stream Parser
⚡ PHP7 / Laravel Multi-format Streaming Parser
Stars: ✭ 391 (-1.51%)
Mutual labels:  laravel
Laravel Trix
Configurable Basecamp Trix Editor (WYSIWYG) delivered to your laravel application
Stars: ✭ 383 (-3.53%)
Mutual labels:  laravel
Comments
Native comments for your Laravel application.
Stars: ✭ 390 (-1.76%)
Mutual labels:  laravel
Laravel Model Cleanup
Clean up unneeded records
Stars: ✭ 388 (-2.27%)
Mutual labels:  laravel
Laravel Schedule Monitor
Monitor scheduled tasks in a Laravel app
Stars: ✭ 393 (-1.01%)
Mutual labels:  laravel
Laravel Eloquent Uuid
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.
Stars: ✭ 388 (-2.27%)
Mutual labels:  laravel
Polr
🚡 A modern, powerful, and robust URL shortener
Stars: ✭ 4,147 (+944.58%)
Mutual labels:  laravel
Closuretable
Adjacency List’ed Closure Table database design pattern implementation for the Laravel framework.
Stars: ✭ 391 (-1.51%)
Mutual labels:  laravel
Laravel Tracy
A Laravel Package to integrate Nette Tracy Debugger
Stars: ✭ 384 (-3.27%)
Mutual labels:  laravel
Pushnotification
PHP and Laravel Package to send push notifications to Android and IOS devices.
Stars: ✭ 395 (-0.5%)
Mutual labels:  laravel
Laravel Relationship Events
Missing relationship events for Laravel
Stars: ✭ 383 (-3.53%)
Mutual labels:  laravel
Flutter background geolocation
Sophisticated, battery-conscious background-geolocation & geofencing with motion-detection
Stars: ✭ 384 (-3.27%)
Mutual labels:  location
Laravel Csp
Set content security policy headers in a Laravel app
Stars: ✭ 388 (-2.27%)
Mutual labels:  laravel
Larapi
An API-friendly fork of Laravel. Authentication, error handling, resource filtering, sorting, pagination and much more included
Stars: ✭ 397 (+0%)
Mutual labels:  laravel

Location

GitHub Actions Scrutinizer Code Quality Latest Stable Version Total Downloads License

Retrieve a user's location from their IP address using external web services, or through a flat-file database hosted on your server.

Requirements

  • PHP >= 7.0
  • Laravel >= 5.0
  • cURL extension enabled

Installation

Install location using composer require:

composer require stevebauman/location

Add the service provider in config/app.php:

Important: If you're using Laravel 5.5 or above, you can skip the registration of the service provider, as it is registered automatically.

Stevebauman\Location\LocationServiceProvider::class

Publish the configuration file (this will create a location.php file inside the config/ directory):

php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"

Usage

Retrieve a client's location

Note: This method retrieves the user's IP address via request()->ip():

use Stevebauman\Location\Facades\Location;

if ($position = Location::get()) {
    // Successfully retrieved position.
    echo $position->countryName;
} else {
    // Failed retrieving position.
}

Retrieve the location of a specific IP address

$position = Location::get('192.168.1.1');

Drivers

Available Drivers

Available drivers at the moment are:

Setting up MaxMind with a self-hosted database (optional)

We encourage setting up MaxMind as a fallback driver using a local database, as it allows you to bypass any throttling that could occur from using external web services.

To set up MaxMind to retrieve the user's location from your own server, you must:

  1. Create a maxmind account.
  2. Sign in.
  3. Click "Download Files" from the left-hand navigation menu.
  4. Download the GeoLite2-City.tar.gz GZIP file.
  5. Extract the downloaded file (you may need to use an application such as 7zip if on Windows).
  6. Create a maxmind folder inside your Laravel application's database directory (database/maxmind).
  7. Place the GeoLite2-City.mmdb file into the maxmind directory. You should end up with a folder path of:
    • my-laravel-app/database/maxmind/GeoLite2-City.mmdb.
  8. Set your default location driver to Stevebauman\Location\Drivers\MaxMind::class, and you're all set!

Note: Keep in mind, you'll need to update this file on a regular basis to retrieve the most current information from clients.

Fallback Drivers

In the config file, you can specify as many fallback drivers as you wish. It is recommended to install the MaxMind database .mmdb file, so you are always retrieving some generic location information for 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.

Creating your own drivers

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($ip)
    {
        return "http://driver-url.com?ip=$ip";
    }
    
    protected function process($ip)
    {
        return rescue(function () use ($ip) {
            $response = json_decode(file_get_contents($this->url($ip)), true);
            
            return new Fluent($response);
        }, $rescue = false);
    }

    protected function hydrate(Position $position, Fluent $location)
    {
        $position->countryCode = $location->country_code;

        return $position;
    }
}

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,

Versioning

Location is versioned under the Semantic Versioning guidelines as much as possible.

Releases will be numbered with the following format:

<major>.<minor>.<patch>

And constructed with the following guidelines:

  • Breaking backward compatibility bumps the major and resets the minor and patch.
  • New additions without breaking backward compatibility bumps the minor and resets the patch.
  • Bug fixes and misc changes bumps the patch.

Minor versions are not maintained individually, and you're encouraged to upgrade through to the next minor version.

Major versions are maintained individually through separate branches.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].