hampel/tlds
Composer 安装命令:
composer require hampel/tlds
包简介
Fetches the latest list of Top Level Domains from IANA, plus Laravel classes for validating TLDs
README 文档
README
This package provides a mechanism for retrieving a list of the current Top Level Domains (TLDs) managed by IANA. It also provides several Laravel classes to validate domain names and TLDs.
By Simon Hampel
Compatibility
- Laravel 9.x - use hampel/tlds 1.10.x
- Laravel 10.x - use hampel/tlds 2.x
- Laravel 11.x - use hampel/tlds 2.1.x
Installation
To install using composer, run the following command:
composer require hampel/tlds
If you want to change the default Tlds configuration, first publish it using the command:
$ php artisan vendor:publish --provider="Hampel\Tlds\TldServiceProvider"
The config files can then be found in config/tlds.php.
Configuration
Refer to the configuration file for more details about configuration options.
tlds.cache.expiry - sets the cache expiry time in seconds
tlds.cache.key - sets the key used to store the TLD data in the cache
tlds.source - set this to 'url' to retrieve the data from a website (eg IANA), set it to 'filesystem' to retrieve the data from a local source (you'll need to configure a Laravel filesystem 'disk' to make this work).
tlds.url - if source is set to 'url', enter the URL to retrieve the data from. By default this is set to the IANA source file
tlds.disk - if source is set to 'filesystem', enter the name of the Laravel filesystem disk you have configured in the 'filesystems.disks' configuration option
tlds.path - if source is set to 'filesystem', enter the path to the data file relative to the root path configured for the disk in the 'fileystems.disks' configuration option (eg. 'tlds/tlds-alpha-by-domain.txt')
Usage
The Tlds package provides a simple mechanism for reading a data file containing a list of Top Level Domains, one per line and returning an array of data. This data may optionally be cached for performance.
The data file can be retrieved directly from the Internet Assigned Numbers Authority (IANA) website, using Guzzle, or if you have a different source or prefer to fetch the data file yourself and then read it from a local source you can reconfigure the Tlds package to read the data file from any Laravel supported filesystem (using Flysystem).
There is also an artisan command available which can be used to fetch the latest data file to refresh the cache. This is ideal for automating the retrieval of data using a cron job or similar.
The simplest way to call the package is using the Facade:
// get a "fresh" copy of the TLD list $tld_array = Tlds::fresh(); // or if you prefer to not use Facades: $tld_array = $app->make(Hampel\Tlds\TldManager::class)->fresh();
This returns a "fresh" copy of the data (bypassing the cache) as an array of TLDs.
To fetch the TLD array from the cache or have it update automatically if the cached data has expired
// get the TLD list from the cache (or update the cache if it has expired) $tld_array = Tlds::get(); // if you prefer to manage the cache yourself, you can do this all manually, for example: if (Cache::has(Config::get('tlds.cache.key')) { $tld_array = Cache::get(Config::get('tlds.cache.key')); } else { Cache::put(Config::get('tlds.cache.key'), Tlds::fresh(), Config::get('tlds.cache.expiry')); }
To run the artisan console command to update the cache:
$ php artisan tld:update Added 725 TLDs to the TLD Cache
Validators
This package adds additional validators for Laravel v10.x and above - refer to Laravel Documentation - Validation for general usage instructions.
Domain
The field under validation must be a valid domain name. The Top Level Domain (TLD) is checked against a list of all acceptable TLDs, including internationalised domains in punycode notation
Example:
use Hampel\Tlds\Validation\Domain; // valid values: example.com; example.au // invalid values: example.invalid-tld $request->validate([ 'domain' => ['required', 'string', new Domain], ]);
DomainIn([<array of TLDs>])
The field under validation must be a valid domain with a TLD from one of the specified options
Example:
use Hampel\Tlds\Validation\DomainIn; // valid values: example.com; example.net // invalid values: example.co; example.au (not in specified list of domain TLDs) $request->validate([ 'domain' => ['required', 'string', new DomainIn(['com', 'net')], ]);
Tld
The field under validation must end in a valid Top Level Domain (TLD). The TLD is checked against a list of all acceptable TLDs, including internationalised domains in punycode notation
If no dots are contained in the supplied value, it will be assumed to be only a TLD.
If the value contains dots, only the part after the last dot will be validated.
Example:
use Hampel\Tlds\Validation\Tld; // valid values: example.com; com; net (either a domain with a valid TLD, or a string that is itself a valid TLD) // invalid values: example.invalid-tld; something-not-a-tld $request->validate([ 'domain' => ['required', 'string', new Tld], ]);
TldIn([<array of TLDs>])
The field under validation must end in a TLD from one of the specified options
If no dots are contained in the supplied value, it will be assumed to be only a TLD.
If the value contains dots, only the part after the last dot will be validated.
Example:
use Hampel\Tlds\Validation\TldIn; // valid values: example.com; example.net; com; net // invalid values: example.co; org (not in specified list of domain TLDs) $request->validate([ 'domain' => ['required', 'string', new TldIn(['com', 'net'])], ]);
hampel/tlds 适用场景与选型建议
hampel/tlds 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.43k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 10 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「domain」 「tld」 「domain name」 「iana」 「top-level-domains」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hampel/tlds 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hampel/tlds 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hampel/tlds 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
OO adaptation of Florian Sager's regdom library for querying Mozilla Public Suffix List
Updated the hostname validator class with the latest TLD's
Build a domain-oriented application on Laravel Framework
Command bus implementation: Commands and domain events
PHP WHOIS provides parsed and raw whois lookup of domains and ASN routes. Based on io-developer/php-whois v4.1.10.
A module manager for Zend Framework which can be used to create configs per domain.
统计信息
- 总下载量: 3.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2014-10-03