Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Maxmind Provider Laravel Package

geocoder-php/maxmind-provider

MaxMind provider for PHP-Geocoder. Integrates MaxMind GeoIP databases to resolve IP addresses into location data (country, region, city, coordinates). Useful for adding fast, offline IP geolocation to your PHP apps.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require geocoder-php/maxmind-provider
    

    Ensure you have the base geocoder-php/geocoder package installed as a dependency.

  2. First Use Case: Basic Geocoding

    use Geocoder\Geocoder;
    use Geocoder\Provider\MaxMind\MaxMindProvider;
    
    $geocoder = new Geocoder();
    $geocoder->registerProvider(new MaxMindProvider('/path/to/GeoLite2-City.mmdb'));
    
    $result = $geocoder->geocode('1600 Amphitheatre Parkway, Mountain View, CA');
    foreach ($result as $hit) {
        echo $hit->getCoordinates()->getLatitude() . ', ' . $hit->getCoordinates()->getLongitude() . "\n";
    }
    
  3. Where to Look First


Implementation Patterns

Common Workflows

  1. Reverse Geocoding

    $coordinates = new \Geocoder\Model\Coordinates(-33.8688, 151.2093);
    $result = $geocoder->reverse($coordinates);
    
  2. Batch Processing

    $ips = ['192.168.1.1', '8.8.8.8'];
    foreach ($ips as $ip) {
        $result = $geocoder->geocode($ip); // Works with IP addresses
    }
    
  3. Integration with Laravel

    // In a service provider or config file
    $geocoder = new Geocoder();
    $geocoder->registerProvider(new MaxMindProvider(config('services.maxmind.path')));
    
    app()->singleton('geocoder', function () use ($geocoder) {
        return $geocoder;
    });
    
  4. Caching Results

    use Geocoder\Cache\DoctrineCache;
    
    $cache = new DoctrineCache();
    $geocoder->registerCache($cache);
    

Best Practices

  • Path Management: Store MaxMind DB paths in .env or config files.
    MAXMIND_DB_PATH=/path/to/GeoLite2-City.mmdb
    
  • Error Handling: Wrap geocoding calls in try-catch blocks for graceful degradation.
    try {
        $result = $geocoder->geocode('invalid address');
    } catch (\Geocoder\Exception\UnsupportedProviderException $e) {
        // Fallback logic
    }
    

Gotchas and Tips

Pitfalls

  1. Database File Paths

    • Hardcoding paths breaks portability. Always use config files or environment variables.
    • Fix: Use config('services.maxmind.path') or env('MAXMIND_DB_PATH').
  2. IP vs. Address Geocoding

    • MaxMind primarily supports IP-based geocoding. For addresses, ensure the provider is correctly configured.
    • Tip: Use Geocoder\Provider\GoogleMapsProvider or Geocoder\Provider\OpenStreetMapProvider for address geocoding if MaxMind is insufficient.
  3. Database Updates

    • MaxMind databases require manual updates. Set up a cron job or Laravel scheduler to refresh them.
    • Example:
      // In a console command
      public function handle() {
          $this->updateMaxMindDatabase();
      }
      
      private function updateMaxMindDatabase() {
          // Logic to download/update the DB file
      }
      
  4. Memory Usage

    • Large MaxMind databases (e.g., GeoLite2-City) can consume significant memory. Optimize by:
      • Using smaller databases (e.g., GeoLite2-City.mmdb instead of GeoIP2).
      • Implementing caching aggressively.

Debugging Tips

  1. Validate Database Files

    use MaxMind\Db\Reader;
    
    try {
        $reader = new Reader('/path/to/database.mmdb');
    } catch (\Exception $e) {
        // Database is corrupted or invalid
    }
    
  2. Log Missing Data

    $result = $geocoder->geocode('123 Unknown St');
    if (empty($result)) {
        \Log::warning('Geocoding failed for: 123 Unknown St', ['provider' => 'maxmind']);
    }
    
  3. Check Provider Registration

    if (!$geocoder->providers()->has('maxmind')) {
        throw new \RuntimeException('MaxMind provider not registered!');
    }
    

Extension Points

  1. Custom Metadata Extraction Override the default metadata handling:

    $hit = $geocoder->geocode('8.8.8.8')->first();
    $metadata = $hit->getExtraProperties(); // Access raw MaxMind data
    
  2. Fallback Providers Combine MaxMind with other providers for redundancy:

    $geocoder->registerProvider(new MaxMindProvider($path));
    $geocoder->registerProvider(new OpenStreetMapProvider());
    
  3. Laravel Service Provider Extend functionality in a Laravel service provider:

    public function register() {
        $this->app->singleton('geocoder', function ($app) {
            $geocoder = new Geocoder();
            $geocoder->registerProvider(new MaxMindProvider($app['config']['services.maxmind.path']));
            return $geocoder;
        });
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor