jeremykendall/php-domain-parser
Parse, validate, and normalize domains, subdomains, and suffixes using the Public Suffix List. Extract registrable domain vs. subdomain, handle IDNs and edge cases, and keep parsing rules current via PSL updates—ideal for URL processing, cookies, and security checks.
Host headers) or sidecars for domain-aware processing.DomainParser class with minimal setup.ServiceProvider or Bootstrap for domain-related events (e.g., DomainParsed).rubysuffix if latency is a concern..test or .example)..bank, .berlin).php-domain-parser/tests).Request facade or URL helper if domain parsing is used in middleware.DomainService).*.example.com vs. example.com)?*.internal.company)?composer require jeremykendall/php-domain-parser.rubysuffix or tldextract.publicsuffix-list (Node.js).domain column as citext) or MongoDB (e.g., nested subdomains array).use JeremyKendall\DomainParser\DomainParser;
$parser = new DomainParser();
$domain = $parser->parse('sub.example.co.uk');
// Output: ['sub' => 'sub', 'domain' => 'example', 'tld' => 'co.uk']
Service class for testability:
namespace App\Services;
use JeremyKendall\DomainParser\DomainParser;
class DomainService {
public function __construct(private DomainParser $parser) {}
public function parse(string $domain): array {
return $this->parser->parse($domain);
}
}
Host headers):
namespace App\Http\Middleware;
use App\Services\DomainService;
class ParseDomainMiddleware {
public function __construct(private DomainService $service) {}
public function handle($request, Closure $next) {
$request->merge(['parsed_domain' => $this->service->parse($request->host)]);
return $next($request);
}
}
FormRequest to validate domains:
use App\Services\DomainService;
use Illuminate\Validation\Rule;
public function rules(): array {
return [
'domain' => ['required', function ($attribute, $value, $fail) {
$parsed = app(DomainService::class)->parse($value);
if (!$parsed['domain']) {
$fail('Invalid domain format.');
}
}],
];
}
äöü.example)."scripts": {
"post-update-cmd": "php-domain-parser:update"
}
Cache::remember()) if parsing is frequent.composer to run php-domain-parser:update post-install.jeremykendall/php-domain-parser to a specific version in composer.json for stability.$parser->parse('example.com', true); // Returns debug info
localhost, 127.0.0.1).| Failure Scenario | Impact | Mitigation | |------------------------------------
How can I help you explore Laravel packages today?