farit-slv/sypex-geo-bundle

An adaptation of SypexGeo library to Symfony Bundle system

Installs: 13

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 10

Type:symfony-bundle

2.0.3 2023-01-10 14:57 UTC

This package is auto-updated.

Last update: 2024-11-10 18:58:14 UTC


README

This is an adaptation of Sypex Geo Library for Symfony.

Installation

Step 1: Download SypexGeoBundle using composer

Add SypexGeoBundle by running the command:

$ composer require farit-slv/sypex-geo-bundle:^2.0

Step 2: Enable the bundle

Enable the bundle in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Yamilovs\Bundle\SypexGeoBundle\SypexGeoBundle(),
    );
}

Step 3: Add some configurations

# app/config/config.yml

faritslv_sypex_geo:
    mode: FILE # FILE (default) | BATCH | MEMORY
    database_path: "%kernel.root_dir%/../var/SypexGeoDatabase/SxGeoCity.dat"

If you need the proxy configuration for database update, you can add:

faritslv_sypex_geo:
    ......
    connection:
        proxy:
            host: 'xxx.xxx.xxx.xxx'
            port: # port number
            
            # You can enable user credentials if you have them
            auth:
                user: 'your username'
                password: 'your password'

Step 4: Download necessary databases

Download necessary databases to database_path.

  • You can run php bin/console faritslv:sypex-geo:update-database-file
  • Or download it manually from Sypex Geo City

Usage

In your controller

<?php
// src/Controller/FooController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Yamilovs\SypexGeo\SypexGeo;

class FooController extends Controller
{
    public function fooAction(Request $request, SypexGeo $sypexGeo)
    {
        $userIp = $request->getClientIp();
        $testIp = '88.86.218.24';

        $city = $sypexGeo->getCity($testIp, true);

        dump($city);
    }
}

Note:

Your local ip address is 127.0.0.1 and Sypex Geo cant get your city or country!

If you want to check data from specific IP address

You can run php bin/console yamilovs:sypex-geo:get-ip-data aa.bb.cc.dd