synortix/laravel-geoip2

Laravel bindings and database updater for MaxMind GeoIp2.

This package's canonical repository appears to be gone and the package has been frozen as a result.

v1.0.0 2019-02-13 09:19 UTC

This package is auto-updated.

Last update: 2019-09-13 11:22:43 UTC


README

Laravel bindings and database updater for MaxMind GeoIp2 Lite.

Simplifies MaxMind database updates and provides out of the box bindings for usage with laravel DI.

Installing

Composer

  • To get started install package by requiring it through composer CLI
composer require synortix/laravel-geoip2:1.*
  • Publish configuration for geoip package
php artisan vendor:publish --provider="Synortix\GeoIp\GeoIpServiceProvider"
  • Run php artisan synortix:geoip:update to fetch latest database

Usage

Using database auto update

Register cron in App\Console\Kernel.php

 protected function schedule(Schedule $schedule)
 {
    ...
    $schedule->command('synortix:geoip:update')->monthly();
    ...
 }

This code would run every month to fetch latest lite database from MaxMind.

Under config/geoip.php you would be able to change url or database name in case it changed on MaxMind side.

Using resolve

/** @var \GeoIp2\Database\Reader $reader */
$reader = resolve(\GeoIp2\ProviderInterface::class);
$reader->country('127.0.0.1');

Using DI


use GeoIp2\Database\Reader;
use GeoIp2\ProviderInterface;

class MyService
{
    /** @var Reader */
    private $geoip;
    
    public function __construct(ProviderInterface $geoip)
    {
      $this->geoip = $geoip;
    }
    
    public function detectCountryCodeUsingIp(string $ip) : string
    {
        return $this->geoip->country('127.0.0.1')->country->isoCode;
    }
}

License

This project is licensed under the MIT License - see the LICENSE.md file for details