galahad/laravel-addressing

Laravel 提供地址功能的包

3.3.0 2024-07-22 17:10 UTC

README

Code coverage status Tests status MIT License Latest stable version

Laravel Addressing

Laravel Addressing 是一个基于 commerceguys/addressing 的 Laravel 包,提供地址功能。它允许您使用 CLDR 和 Google 的数据轻松处理地址。

Laravel Addressing 的主要重点是

  • 访问最新版的国列表、州/省等
  • 轻松验证地址数据
  • 格式化地址以符合特定国家的规则

如果您觉得这个包很有用,您应该检查我们的通用地址输入 Franklin,它与 Laravel Addressing 遵循相同的约定。

安装

首先,安装 composer 包

composer require galahad/laravel-addressing

基本用法

国家

$country = Addressing::country('US');
echo $country->getName(); // "United States"
echo $country->getCountryCode(); // "US"

行政区域(通常是州或省)

$usa = Addressing::country('US');

echo $usa->administrativeArea('AL')->getName(); // "Alabama"
echo $usa->administrativeArea('AL')->getCode(); // "AL"

typeof $usa->administrativeAreas() // AdministrativeAreaCollection

验证器

您可以在您的 Laravel 应用程序中使用一些自定义验证器

国家

您可以使用 countrycountry_codecountry_name 验证国家输入

$this->validate($request, [
    'country' => 'required|country_code', // Must be a 2-letter ISO code, such as "US"
]);

$this->validate($request, [
    'country' => 'required|country_name', // Must be the full country name, such as "United States"
]);

$this->validate($request, [
    'country' => 'required|country', // Must be the full country name or 2-letter ISO code
]);

行政区域

您可以使用 administrative_areaadministrative_area_codeadministrative_area_name 验证行政区域输入

// "country_field" is the form input that represents the country to validate against

$this->validate($request, [
    'state' => 'required|administrative_area_code:name_of_country_field',
]);

$this->validate($request, [
    'state' => 'required|administrative_area_name:country_field',
]);

$this->validate($request, [
    'state' => 'required|administrative_area:country_field',
]);

邮政编码

您可以使用 postal_code 验证邮政编码

$this->validate($request, [
    'postal_code' => 'required|postal_code:country_field,administrative_area_field',
]);

HTTP 端点

Laravel Addressing 默认发布两个路由,这些路由可以在配置文件中禁用。前缀 (/galahad/addressing) 也可以配置。

GET /galahad/addressing/countries

{
    "label": "Countries",
    "options": {
        "AF": "Afghanistan",
        "..": "...",
        "ZW": "Zimbabwe"
    }
}

GET /galahad/addressing/countries/us/administrative-areas

{
     "label": "States",
     "country_code": "US",
     "options": {
        "AL": "Alabama",
        "**": "*******",
        "WY": "Wyoming"
     }
}

谢谢!

特别感谢 Commerce Guys 及其出色的 addressingintl 包,本项目严重依赖于这些包。