nnjeim/world

Laravel 国家、州、城市和货币

安装次数: 91,793

依赖: 2

建议者: 0

安全: 0

星标: 750

关注者: 13

分支: 104

开放问题: 0

类型:laravel-package

1.1.31 2024-07-15 03:36 UTC

This package is auto-updated.

Last update: 2024-09-25 11:19:22 UTC


README

Laravel world

The World 是一个 Laravel 扩展包,提供国家、州、城市、时区、货币和语言列表。

可以通过 World Facade 或定义的 API 路由使用它。

安装

set APP_ENV=local
composer require nnjeim/world

设置环境变量 WORLD_DB_CONNECTION 为所需的数据库连接(可选)

world:install 命令是一个辅助工具,用于自动化安装过程

php artisan world:install

您可以按照以下步骤手动安装此包(可选)

php artisan vendor:publish --tag=world

php artisan migrate

php artisan db:seed --class=WorldSeeder # (requires ~15min)

1.1.31 版本的新增功能是什么?

  • 添加了 fa 区域 @DevNull-IR
  • 添加了马来西亚的城市 @hirenkeraliya

变更日志

请阅读 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 Nnjeim\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 Nnjeim\World\World;

$action =  World::countries([
	'fields' => 'states,cities',
	'filters' => [
		'iso2' => 'FR',
	]
]);

if ($action->success) {

	$countries = $action->data;
}

响应

(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 Nnjeim\World\WorldHelper;

new class {
    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 路由

所有路由都可以由任何字符串前缀。例如:adminapi...

国家

城市

时区

货币

语言

本地化

可用的区域设置有 ar、bn、br、de、en、es、fr、it、ja、kr、nl、pl、pt、ro、ru、tr 和 zh。
默认区域设置为 en。

包含在请求头中

accept-language=locale

或者,您可以使用特定区域设置与World外观的setLocale('locale')辅助方法。示例

World::setLocale('zh')->countries();

模式

配置

世界包的配置位于world.php配置文件中。
如果您是从旧版本升级,应考虑通过执行以下操作重新发布文件

php artisan vendor:publish --tag=world --force

自定义数据库连接

默认情况下,此包使用默认数据库连接,但您可以使用.env文件中的WORLD_DB_CONNECTION变量来自定义它。

国家限制

在数据库生成时,可以通过在allowed_countriesdisallowed_countries数组列表中添加ISO2国家代码来限制国家。

支持的区域设置

一个与本地化lang/文件相关的已接受区域设置的列表。

模块启用

状态、城市、时区、货币和语言模块可以选择禁用。
请注意,城市模块依赖于状态模块。

路由

如果您不希望将包用作API服务,可以通过将routes赋值为false来禁用所有路由。

迁移

它提供了启用或禁用数据库字段的能力。
更改此配置时,应删除数据库并重新运行生成器。

测试

要求

  • 数据库已填充。
  • 数据库连接在.env文件中定义。

浏览到包根目录并运行

composer install # installs the package dev dependencies
composer test

* 可选