eka / world
Laravel 国家、州、城市和货币。由 Eka/world 衍生而来
Requires
- php: >=7.4
Requires (Dev)
- orchestra/testbench: >=v4.0.0
- phpunit/phpunit: >=8.5.8
README
The World 是一个 Laravel 扩展包,提供国家、州、城市、时区、货币和语言的列表。
它可以通过 World Facade 或定义的 API 路由来使用。
安装
composer require eka/world
php artisan vendor:publish --tag=world
php artisan migrate
php artisan db:seed --class=WorldSeeder (requires ~15min)
v1.1.28 新增功能
- 将 state_code 表的名称长度增加到 5 个字符,以避免在种植新数据库时可能出现的潜在错误。
变更日志
请阅读 CHANGELOG 了解最近更改的详细信息。
贡献
请阅读 CONTRIBUTING 获取更多详细信息。
演示
请随时查询 https://laravel-world.com
示例
https://laravel-world.com/api/countries
https://laravel-world.com/api/countries?search=rom
https://laravel-world.com/api/states?filters[country_code]=RO&fields=cities
用法
列出所有国家。
使用 World Facade
use eka\World\World;
$action = World::countries();
if ($action->success) {
$countries = $action->data;
}
response (object)
{
"success": true,
"data": [
{
"id": 1,
"name": "Afghanistan"
},
{
"id": 2,
"name": "Åland Islands"
},
.
.
.
],
}
使用 API 国家端点
https://myDomain.local/api/countries
获取具有其州和城市的国家。
使用 World Facade
use eka\World\World;
$action = World::countries([
'fields' => 'states,cities',
'filters' => [
'iso2' => 'FR',
]
]);
if ($action->success) {
$countries = $action->data;
}
response (object)
{
"success": true,
"data": [
"id": 77,
"name": "France",
"states": [
{
"id": 1271,
"name": "Alo"
},
{
"id": 1272,
"name": "Alsace"
},
.
.
.
],
"cities": [
{
"id": 25148,
"name": "Abondance"
},
{
"id": 25149,
"name": "Abrest"
},
.
.
.
]
],
}
使用 API 国家端点
https://myDomain.local/api/countries?fields=states,cities&filters[iso2]=FR
按国家 ID 列出所有城市。
use eka\World\WorldHelper;
protected $world;
public function __construct(WorldHelper $world) {
$this->world = $world;
}
$action = $this->world->cities([
'filters' => [
'country_id' => 182,
],
]);
if ($action->success) {
$cities = $action->data;
}
使用 API 城市端点
https://myDomain.local/api/cities?filters[country_code]=RO
可用操作
操作响应如下
- success (布尔值)
- message (字符串)
- data (Illuminate\Support\Collection 实例)
- errors (数组)
国家操作
- fields*: 以逗号分隔的字符串(除了国家、州、城市、货币和时区之外的国家表字段)。
- filters*: 键(国家表字段)及其对应值的数组。
- search*: 字符串。
州操作
- fields*: 以逗号分隔的字符串(除了国家和州之外的国家表字段)。
- filters*: 键(州表字段)及其对应值的数组。
- search*: 字符串。
城市操作
- fields*: 以逗号分隔的字符串(除了国家和州之外的城市表字段)。
- filters*: 键(城市表字段)及其对应值的数组。
- search*: 字符串。
时区操作
- fields*: 以逗号分隔的字符串(除了国家之外时区表字段)。
- filters*: 键(时区表字段)及其对应值的数组。
- search*: 字符串。
货币操作
- fields*: 以逗号分隔的字符串(除了国家之外货币表字段)。
- filters*: 键(货币表字段)及其对应值的数组。
- search*: 字符串。
语言操作
- fields*: 以逗号分隔的字符串(语言表字段)。
- filters*: 键(语言表字段)及其对应值的数组。
- search*: 字符串。
可用 API 路由
所有路由都可以由任何字符串前缀。例如 admin、api、api ...
国家
州
城市
时区
货币
语言
本地化
可用的区域设置为 ar、bn、br、de、en、es、fr、it、ja、kr、nl、pl、pt、ro、ru、tr 和 zh。
默认区域设置为 en。
包含在请求头中
accept-language=locale
或者,您可以使用特定的区域设置与 World
Facade 的 setLocale('locale')
辅助方法。示例
World::setLocale('zh')->countries();
模式
配置
世界包的配置位于 world.php 配置文件中。
如果您是从旧版本升级,您应该考虑通过执行以下操作来重新发布文件:
php artisan vendor:publish --tag=world --force
国家限制
在种植数据库时可以限制国家,通过在 allowed_countries 或 dissallowed_countries 数组列表中添加 iso2 国家代码。
支持的区域设置
与本地化语言文件相关联的已接受区域设置的列表。
模块启用
国家、城市、时区、货币和语言模块可以可选禁用。
请注意,城市模块依赖于国家模块。
路由
如果您不希望将包用作API服务,可以通过将routes设置为false来禁用所有路由。
迁移
它提供了启用或禁用数据库字段的能力。
更改此配置时,应删除数据库并重新运行seeder。
测试
要求
- 数据库已初始化。
- 数据库连接定义在.env文件中。
浏览到包根目录并运行
composer install //installs the package dev dependencies
composer test
* 可选