yamilovs/sypex-geo-bundle

SypexGeo库对Symfony Bundle系统的适配

安装次数: 43,221

依赖者: 4

建议者: 0

安全性: 0

星标: 8

关注者: 3

分支: 10

开放问题: 0

类型:symfony-bundle

2.0.1 2020-03-06 12:00 UTC

This package is auto-updated.

Last update: 2024-09-20 01:24:03 UTC


README

Build Status

SypexGeoBundle

这是为Symfony适配的 Sypex Geo库

安装

步骤 1: 使用composer下载SypexGeoBundle

运行以下命令添加SypexGeoBundle

$ composer require yamilovs/sypex-geo-bundle:^2.0

步骤 2: 启用bundle

在kernel中启用bundle

<?php
// app/AppKernel.php

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

步骤 3: 添加一些配置

# app/config/config.yml

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

如果您需要数据库更新的代理配置,可以添加

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'

步骤 4: 下载必要的数据库

将必要的数据库下载到 database_path

  • 您可以运行 php bin/console yamilovs:sypex-geo:update-database-file
  • 或从 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);
    }
}

注意

您的本地IP地址是 127.0.0.1,Sypex Geo无法获取您的城市或国家!

如果您想检查特定IP地址的数据

您可以运行 php bin/console yamilovs:sypex-geo:get-ip-data aa.bb.cc.dd