- How do I install spatie/dns in a Laravel project?
- Run `composer require spatie/dns` in your project directory. Ensure `dig` is installed on Linux/macOS or use `dns_get_record()` on Windows. The package has no Laravel-specific dependencies and works in any PHP 8.0+ environment.
- Which Laravel versions does spatie/dns support?
- The package requires PHP 8.0+ and is compatible with Laravel 8, 9, and 10. It’s framework-agnostic but integrates seamlessly with Laravel’s Service Providers, Facades, or direct instantiation in controllers.
- Can I filter DNS records by type (e.g., only MX or TXT)?
- Yes. Use `getRecords('domain.com', 'MX')` for a single type or pass an array like `['A', 'CNAME']`. You can also use constants like `DNS_MX` or bitwise operators (e.g., `DNS_A | DNS_AAAA`) for flexibility.
- How do I cache DNS responses in Laravel to avoid repeated queries?
- Cache responses manually using Laravel’s cache system (e.g., Redis) with a TTL matching the DNS record’s TTL. Example: `Cache::remember('dns:spatie.be', now()->addMinutes(3600), fn() => $dns->getRecords('spatie.be'))`.
- Does spatie/dns work in shared hosting or serverless environments?
- It depends on your environment. Shared hosting may lack `dig` (Linux/macOS) or `dns_get_record()` (Windows). Test with `php artisan dns:check` or verify `dig` availability via `exec('dig +short example.com')` before deployment.
- How do I mock DNS responses for testing in PHPUnit?
- Use the `Factory::guess()` method to simulate records. Example: `Factory::guess('A', '192.0.2.1')->create()` returns a mock record. Combine with Laravel’s `Mockery` or `createMock()` for full control over test scenarios.
- Can I extend spatie/dns to log or transform DNS records?
- Yes. Implement the `Handler` interface to customize behavior (e.g., logging, caching, or converting records to Eloquent models). Register your handler via the Service Provider: `$dns->setHandler(new CustomHandler())`.
- What are the performance implications of blocking DNS queries in Laravel?
- DNS queries are synchronous by default, which can slow down high-traffic apps. Mitigate this by caching responses (Redis) or offloading queries to queues (e.g., Laravel Jobs) for async processing. Monitor timeout/retries for unreliable networks.
- How do I validate email domains or SPF/DKIM records with this package?
- Fetch MX records for domain validation: `$dns->getRecords('example.com', 'MX')`. For SPF/DKIM, check TXT records: `$records = $dns->getRecords('example.com', 'TXT');` then parse the `data()` property for SPF/DKIM tags like `v=spf1`.
- Are there alternatives to spatie/dns for Laravel DNS lookups?
- Yes. For lightweight use, consider `dns_get_record()` (built into PHP). For advanced features, explore `rubix/ml-dns` (pure PHP) or `league/dns` (more complex but cross-platform). Spatie’s package stands out for its Laravel-friendly API and record accessors.