codeheures / laravel-utils
适用于 Laravel 5.4 的工具
Requires
- php: >=7.0.0
- geoip2/geoip2: ~2.8
- illuminate/support: ~5.5
- moneyphp/money: ^3.1
README
Laravel-utils 是一个工具箱,允许
- API 通过 IP 获取客户端的地理位置信息。适用于服务器端处理
- PHP 工具用于获取有效的公网 IP(在开发环境中很有用)
- PHP 工具用于获取浏览器信息
- PHP 工具用于获取数据库枚举列表,或使用可重用的多个 where (=) 条件获取计数项
- 中间件用于共享 config('runtime.ip') 变量(基于获取有效公网 IP 的 PHP 工具)
- 中间件用于自动获取最佳客户端语言环境,并通过 config('runtime.locale') 变量在您的应用程序中共享。您可以使用 {lang} 作为路由参数强制设置语言环境
- 中间件用于自动获取最佳货币单位,并通过 config('runtime.currency') 变量在您的应用程序中共享
API:通过 IP 获取地理位置信息
此 API 允许您向客户端提供地理位置信息(适用于 xhtmlrequests)。
您还可以使用静态函数进行服务器端处理。
获取完整的地理位置信息
在 URL 上使用 IP
http://yourproject/geoIp/geoByIp/82.246.117.210
//result example {"ip":"82.246.117.210","city":"Joué-lès-Tours","region":"Centre","country":"FR","loc":"47.3522,0.6691","postal":"37300"}
不使用 IP:首先尝试使用您的公网 IP。如果不是公网 IP,则尝试使用 config('codeheuresUtils.ipTest')。如果不可用,最后使用静态 IP
http://yourproject/geoIp/geoByIp
//result example {"ip":"82.246.117.210","city":"Joué-lès-Tours","region":"Centre","country":"FR","loc":"47.3522,0.6691","postal":"37300"}
仅获取 LOC [纬度,经度] 信息
在 URL 上使用或不用 IP(见上方)
http://yourproject/geoIp/geoLocByIp
//result example ["47.3522","0.6691"]
仅获取国家信息
在 URL 上使用或不用 IP(见上方)
http://yourproject/geoIp/countryByIp
//result example "FR"
如果出错
//result example {"error":"unknow IP"}
PHP 工具:通过 IP 获取地理位置信息
您可以使用静态类进行服务器端处理
$ip = "82.246.117.210" if(filter_var($ip, FILTER_VALIDATE_IP)) { try { [ 'all_infos' => Codeheures\LaravelUtils\Traits\Geo\GeoUtils::getGeoByIp($ip), 'loc_infos' => Codeheures\LaravelUtils\Traits\Geo\GeoUtils::getGeoLocByIp($ip), 'country_info' => Codeheures\LaravelUtils\Traits\Geo\GeoUtils::getCountryByIp($ip) ] } catch(...) { ...} }
//result example [ 'all_infos' => ["ip"=>"82.246.117.210","city"=>"Joué-lès-Tours","region"=>"Centre","country"=>"FR","loc"=>"47.3522,0.6691","postal"=>"37300"], 'loc_infos' => ["47.3522","0.6691"], 'country_info' => "FR" ]
PHP 工具:获取有效公网 IP
仅请求:如果公网 IP,则返回您的 IP,否则返回静态类变量 Ip
$ip = Codeheures\LaravelUtils\Traits\Tools\Ip::getNonPrivateIpByRequest($request));
//result example "82.246.117.210"
带有第二个参数:如果公网 IP,则返回您的 IP,或参数(如果公网 IP),最后返回静态类变量 Ip
$ip = Codeheures\LaravelUtils\Traits\Tools\Ip::getNonPrivateIpByRequest($request, '82.246.117.210'));
//result example "82.246.117.210"
PHP 工具:获取浏览器名称
$browser = Codeheures\LaravelUtils\Traits\Tools\Browser::getBrowserName();
//result example "chrome"
PHP 工具:获取数据库信息
获取表中某列的枚举值列表
示例:获取 priceTable 表中货币列的枚举值
$listEnumValues = Codeheures\LaravelUtils\Traits\Tools\Database::getEnumValues('priceTable','currencies');
//result example ['dollard', 'euro', 'yen', 'bitcoins']
使用多个 count where 条件轻松获取表中的元素计数
where 子句是等值条件
Codeheures\LaravelUtils\Traits\Tools\Database::getCountItems('customer',['name'=>'paul', 'age'=>'18']);
//result example 172
PHP 工具:语言环境工具
语言环境工具允许您获取各种语言环境信息。此工具套件用于强大的中间件,以找到最适合用户的最佳语言环境。当中间件找到最佳语言环境时,它将在 config('runtime.locale') 变量中共享,该变量可在整个应用程序中访问。您可以使用它来为用户翻译应用程序。
服务器上可用的语言环境数组
没有语言限制(在 codeheuresUtils.availableLocales 配置文件中查找)
Codeheures\LaravelUtils\Traits\Tools\Locale::listLocales();
//result example [ "af" => [ "code" => "af", "name" => "afrikaans", "region" => "" ], "af_NA" => [ "code" => "af_NA", "name" => "afrikaans (Namibie)", "region" => "namibie" ], ... ]
有限制
Codeheures\LaravelUtils\Traits\Tools\Locale::listLocales();
//result example [ "en" => [ "code" => "en", "name" => "anglais", "region" => "" ] "en_001" => [ "code" => "en_001", "name" => "anglais (Monde)", "region" => "monde" ] "en_150" => [ "code" => "en_150", "name" => "anglais (Europe)", "region" => "europe" ] ... ]
布尔测试以确定语言环境是否可用
Codeheures\LaravelUtils\Traits\Tools\Locale::existLocale('fr_FR');
//result example true
测试语言环境是否有效,并返回语言环境或 null
Codeheures\LaravelUtils\Traits\Tools\Locale::isValidLocale('fr_FR');
//result example "fr_FR"
通过国家代码获取第一个可能的语言环境
Codeheures\LaravelUtils\Traits\Tools\Locale::getFirstLocaleByCountryCode('ca');
//result example "en_CA"
组合语言环境并返回如果存在,不存在则返回 null
Codeheures\LaravelUtils\Traits\Tools\Locale::composeLocale('fr', 'CA');
//result example "fr_CA"
获取默认语言环境,在 env('DEFAULT_LOCALE') 或静态变量 $last_fall_locale 中
Codeheures\LaravelUtils\Traits\Tools\Locale::getDefaultLocale();
//result example "en_US"
PHP 工具:货币工具
货币工具允许您获取各种货币信息。它使用MoneyPhp和config('runtime.locale')。此工具套件用于强大的中间件,以找到最适合用户的货币。当中间件找到最佳货币时,它会在config('runtime.currency')变量中共享,该变量在应用程序中随处可见。您可以使用它来管理应用程序中的价格。
测试货币是否存在
Codeheures\LaravelUtils\Traits\Tools\Currencies::isAvailableCurrency("EUR")
//result example true
获取货币的子单位(如果存在则返回0)
Codeheures\LaravelUtils\Traits\Tools\Currencies::getSubUnit("EUR")
//result example 2
根据区域设置获取带符号的货币列表
Codeheures\LaravelUtils\Traits\Tools\Currencies::listCurrencies("fr_CA")
//result example [ "XAF" => [ "code" => "XAF", "symbol" => "XAF", "subunit" => 0 ], "CAD" => [ "code" => "CAD", "symbol" => "$", "subunit" => 2 ], ... ]
根据货币代码和区域设置获取符号
Codeheures\LaravelUtils\Traits\Tools\Currencies::getSymbolByCurrencyCode("CAD", "fr)
//result example "$CA"
通过组合区域设置获取默认货币(第二个参数是回退货币)
Codeheures\LaravelUtils\Traits\Tools\Currencies::getDefaultMoneyByComposeLocale("fr_CA", "EUR")
//result example "CAD"
在env('DEFAULT_CURRENCY')或静态变量$last_fall_currency中获取默认货币
Codeheures\LaravelUtils\Traits\Tools\Locale::getDefaultCurrency();
//result example "EUR"
中间件:config('runtime.ip'),config('runtime.locale') & config('runtime.currency')
安装中间件允许您在应用程序控制器和视图中使用config('runtime.ip'),config('runtime.locale') & config('runtime.currency')
config('runtime.locale') & config('runtime.currency')可以保存在User属性'locale' & 'currency'中并读取
config('runtime.ip') //your public Ip or fallback ip (see above PHP Tool: Get valid public IP) config('runtime.locale') //the RuntimeLocale middlware look for the best local and assign to this config('runtime.currency') //the RuntimeLocale middlware look for the best currecny and assign to this
安装
- 将laravel-utils添加到您的composer.json文件中,以要求laravel-utils
require : {
"laravel/framework": "5.4.*",
"codeheures/laravel-utils": "^1.0"
}
- 更新Composer
composer update
- 将服务提供者添加到config/app.php中
Codeheures\LaravelUtils\LaravelUtilsServiceProvider::class,
- 将中间件添加到app/Http/kernel.php中的web路由中(尊重顺序IP->Locale->Currency)。Middleware SetLocaleByRuntime是可选的,允许您根据检测到的最佳区域设置自动设置应用程序的区域设置。
protected $middlewareGroups = [
'web' => [
...
\Codeheures\LaravelUtils\Http\Middleware\RuntimeIp::class,
\Codeheures\LaravelUtils\Http\Middleware\RuntimeLocale::class,
\Codeheures\LaravelUtils\Http\Middleware\RuntimeCurrency::class,
\Codeheures\LaravelUtils\Http\Middleware\SetLocaleByRuntime::class, //optionnal
],
- 发布配置文件config/codeheuresUtils.php
php artisan vendor:publish --provider="Codeheures\LaravelUtils\LaravelUtilsServiceProvider"
在配置文件中可以更改
- 路由URI
- 路由名称
- 路由中间件(强烈建议添加一个admin类型的中间件来刷新Db)
- IP测试值
- 可用区域设置列表
可选:将"locale"和/或"currency"属性添加到User中,以在您的应用程序中管理保存首选项区域设置
首次使用之前,我们更新maxmind db
http://yourproject/geoIp/refreshDb
恭喜,您已成功安装laravel-utils!
版权和许可
本产品包含由MaxMind创建的GeoLite2数据,可在http://www.maxmind.com获取。
本产品使用GeoIp2在apache2许可证下https://github.com/maxmind/GeoIP2-php。
本产品使用Moneyphp在MIT许可证下https://github.com/moneyphp/money。
本软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括软件包、其适销性或其适用于任何特定目的的适用性。
这是免费软件,许可协议为Apache License,版本2.0。