Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Domain Parser Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install via Composer

    composer require jeremykendall/php-domain-parser
    
    • No additional configuration is required for basic usage.
  2. First Use Case: Parsing a Domain

    use JeremyKendall\DomainParser\DomainParser;
    
    $parser = new DomainParser();
    $domain = $parser->parse('example.co.uk');
    
    // Outputs:
    // [
    //   'domain' => 'example.co.uk',
    //   'subdomains' => ['example'],
    //   'mainDomain' => 'example.co.uk',
    //   'tld' => 'co.uk',
    //   'publicSuffix' => 'co.uk',
    //   'effectiveTld' => 'uk',
    //   'effectiveTldPlusOne' => 'co.uk',
    //   'isValid' => true,
    // ]
    
  3. Where to Look First

    • Documentation (if available) for advanced use cases.
    • DomainParser class for core functionality.
    • DomainParser::parse() method for basic parsing.
    • DomainParser::isValid() for validation checks.

Implementation Patterns

Common Workflows

  1. Extracting Subdomains

    $subdomains = $parser->parse('sub.example.co.uk')['subdomains'];
    // ['sub', 'example']
    
  2. Public Suffix List (PSL) Integration

    • Automatically uses the latest PSL (Public Suffix List) for accurate parsing.
    • Update PSL manually via:
      $parser->updatePublicSuffixList();
      
  3. Validation & Sanitization

    if ($parser->isValid('invalid..domain')) {
        // Process valid domain
    }
    
  4. Extracting TLDs

    $tld = $parser->parse('blog.example.com')['effectiveTld']; // 'com'
    $tldPlusOne = $parser->parse('blog.example.com')['effectiveTldPlusOne']; // 'example.com'
    
  5. Batch Processing

    $domains = ['example.com', 'test.co.uk', 'invalid'];
    $results = array_map([$parser, 'parse'], $domains);
    

Integration Tips

  • Laravel Request Handling Useful for extracting domains from Request objects:
    use Illuminate\Http\Request;
    
    $domain = $parser->parse(Request::server('HTTP_HOST'));
    
  • Middleware for Domain-Based Logic
    public function handle(Request $request, Closure $next) {
        $parsed = $parser->parse($request->getHost());
        if ($parsed['effectiveTld'] === 'com') {
            // Apply logic for .com domains
        }
        return $next($request);
    }
    
  • Caching Parsed Results Store parsed domains in cache to avoid reprocessing:
    $cacheKey = 'parsed:'.$domain;
    $parsed = cache()->remember($cacheKey, now()->addHours(1), function() use ($parser, $domain) {
        return $parser->parse($domain);
    });
    

Gotchas and Tips

Pitfalls

  1. PSL Updates

    • The package ships with a bundled PSL, but it may not always be the latest.
    • Fix: Manually update via updatePublicSuffixList() or configure auto-updates (if supported in future versions).
  2. Edge Cases in Parsing

    • Domains with unusual TLDs (e.g., example.xn--p1ai for example.рф) may require manual handling.
    • IP addresses (e.g., 192.168.1.1) will return isValid: false.
  3. Case Sensitivity

    • Parsing is case-insensitive, but output may vary (e.g., CO.UK vs co.uk). Normalize if needed:
      $normalized = strtolower($parsed['domain']);
      
  4. Performance with Large Batches

    • Parsing thousands of domains in a loop can be slow.
    • Optimization: Use array_map with parallel processing (e.g., Laravel’s parallel() helper).

Debugging Tips

  • Verify PSL Version
    echo $parser->getPublicSuffixListVersion();
    
  • Check for Malformed Input
    if (!$parser->isValid($domain)) {
        log("Invalid domain: {$domain}");
    }
    
  • Inspect Full Parsed Data
    dd($parser->parse('complex.example.co.uk')); // Debug complex cases
    

Extension Points

  1. Custom PSL Rules Override the default PSL by extending DomainParser:

    class CustomDomainParser extends DomainParser {
        protected function getPublicSuffixList(): string {
            return file_get_contents('path/to/custom-psl.txt');
        }
    }
    
  2. Adding Domain-Specific Logic Use parsed data to trigger custom logic:

    $parsed = $parser->parse($domain);
    if (in_array('shop', $parsed['subdomains'])) {
        // Redirect to shopping cart
    }
    
  3. Testing Edge Cases Write tests for:

    • Internationalized domain names (IDNs).
    • New TLDs (e.g., .ai, .tech).
    • Subdomains with special characters (if allowed).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui