tapfury/laravel-phone
Composer 安装命令:
composer require tapfury/laravel-phone
包简介
Adds a phone validator to Laravel and Lumen based on Google's libphonenumber API.
README 文档
README
Adds a phone validator & formatter to Laravel 4 & 5 and Lumen based on the PHP port of Google's libphonenumber API by giggsey.
Installation
Run the following command to install the latest version of the package
composer require propaganistas/laravel-phone
Laravel
In your app config, add the Service Provider to the $providers array (only for Laravel 5.4 or below):
'providers' => [ ... Propaganistas\LaravelPhone\LaravelPhoneServiceProvider::class, ],
In your languages directory, add for each language an extra language line for the validator:
"phone" => "The :attribute field contains an invalid number.",
Lumen
In bootstrap/app.php, register the Service Provider
$app->register(Propaganistas\LaravelPhone\LaravelPhoneServiceProvider::class);
Validator
To validate a field using the phone validator, use the phone keyword in your validation rules array. The phone validator is able to operate in three ways.
-
You either specify ISO 3166-1 alpha-2 compliant country codes yourself as parameters for the validator, e.g.:
'phonefield' => 'phone:US,BE',
The validator will check if the number is valid in at least one of provided countries, so feel free to add as many country codes as you like.
-
You don't specify any parameters but you plug in a dedicated country input field (keyed by ISO 3166-1 compliant country codes) to allow end users to supply a country on their own. The easiest method by far is to install the Laravel-Intl package. Make sure the country field is named similar as the phone field but with _country appended for automatic discovery, or provide your custom country field name as a parameter to the validator:
'phonefield' => 'phone', 'phonefield_country' => 'required_with:phonefield',
'phonefield' => 'phone:custom_country_field', 'custom_country_field' => 'required_with:phonefield',
If using Laravel-Intl, you could then use the following snippet to populate a country selection list. It will automatically present translated country names according to your application locale:
Country::all() -
You instruct the validator to detect which country the number belongs to using the
AUTOkeyword (and optionally any fallback countries):'phonefield' => 'phone:AUTO,US',
The validator will try to extract the country from the number itself and then check if the number is valid for that country. If the country could not be guessed it will be validated using the fallback countries if provided. Note that country guessing will only work when phone numbers are entered in international format (prefixed with a
+sign, e.g. +32 ....). Leading double zeros will NOT be parsed correctly as this isn't an established consistency.
To specify constraints on the number type, just append the allowed types to the end of the parameters, e.g.:
'phonefield' => 'phone:US,BE,mobile',
The most common types are mobile and fixed_line, but feel free to use any of the types defined here.
You can also enable more lenient validation (for example, fixed lines without area codes) by using the LENIENT parameter. This feature inherently doesn't play well with country autodetection and number type validation, so use such combo at own risk.
'phonefield' => 'phone:LENIENT,US',
Formatter
Format a phone number using the phone() helper function. $country_code is the country the phone number belongs to.
phone($phone_number, $country_code = null, $format = PhoneNumberFormat::INTERNATIONAL)
Always try to provide a $country_code in order for the phone number to be recognized correctly. If no $country_code was given, the current application locale will be used as a sensible default.
The $format parameter defines the output format and is optional. The format defaults to international format but can be any constant of libphonenumber\PhoneNumberFormat.
tapfury/laravel-phone 适用场景与选型建议
tapfury/laravel-phone 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.2k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「phone」 「laravel」 「libphonenumber」 「lumen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tapfury/laravel-phone 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tapfury/laravel-phone 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tapfury/laravel-phone 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Phone helper & validator for Nette Framework
Yii2 phone formatter and validator.
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 3.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-19