sewidan/world

Laravel 国家、州、城市和货币

安装: 21

依赖者: 0

建议者: 0

安全性: 0

星星: 0

关注者: 1

分支: 105

类型:laravel-package

v1.0.3 2024-07-14 16:23 UTC

This package is auto-updated.

Last update: 2024-09-14 16:55:42 UTC


README

Laravel world

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

可以通过 World Facade 或定义好的 Api 路由来使用。

安装

composer require sewidan/world

php artisan vendor:publish --tag=world

php artisan migrate

php artisan db:seed --class=WorldSeeder  //if you need to seed all world countries and cities
php artisan db:seed --class=CountriesSeeder  //if you need to seed only world countries
php artisan install:country {country ID}  //if you need to seed cities by country ID

升级到 v1.1.12 吗?

  • 新功能: search 参数。
  • 新功能:在配置中添加了允许国家和不允许国家数组。此新功能由 @aeq-dev 提供,旨在帮助限制国家及其依赖的填充。
  • 如果您从低于 1.1.10 的版本升级,并且由于路由和配置文件的变化,请通过执行命令 php artisan vendor:publish --tag=world --force 重新发布包资源。
  • 如有需要,在 world.php 配置文件中自定义表名并启用或禁用可选的数据库字段。
  • 国家表子区域字段已从 sub_region 重命名为 subregion。建议刷新迁移和填充数据库表。

更新日志

请阅读 更新日志 了解最近更改的详细信息。

贡献

请阅读 贡献指南 了解更多详情。

演示

请自由查询 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 
{
	"success": true,
	"message": "countries",
	"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;
}

response 
{
	"success": true,
	"message": "countries",
	"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;

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、ja、kr、nl、pl、pt、ro、ru 和 zh。
默认地区是 en。
包含在请求头中

accept-language=locale

或者,您可以使用带有World Facade的setLocale('locale')辅助方法来指定特定区域。示例

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

架构

配置

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

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

支持的区域

一个与本地化语言文件相关的已接受区域列表。

模块启用

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

路由

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

迁移

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

测试

需求

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

浏览到包根目录并运行

composer install //installs the package dev dependencies
composer test

* 可选