This is an adaptation of Sypex Geo Library for Symfony.
Add SypexGeoBundle by running the command:
$ composer require yamilovs/sypex-geo-bundle:^2.0
Enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Yamilovs\Bundle\SypexGeoBundle\SypexGeoBundle(),
);
}
# app/config/config.yml
yamilovs_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:
yamilovs_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'
Download necessary databases to database_path
.
- You can run
php bin/console yamilovs:sypex-geo:update-database-file
- Or download it manually from Sypex Geo City
<?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!
You can run php bin/console yamilovs:sypex-geo:get-ip-data aa.bb.cc.dd