digipolisgent/domainator9k-core-bundle
Installation Add the bundle to your Symfony/Laravel project via Composer:
composer require district09/domainator9k-core-bundle
Register the bundle in config/bundles.php (Symfony) or config/app.php (Laravel via Symfony bridge):
return [
// ...
District09\Domainator9kCoreBundle\Domainator9kCoreBundle::class => ['all' => true],
];
First Use Case: Domain Validation
Inject the DomainValidator service into a controller or service:
use District09\Domainator9kCoreBundle\Validator\DomainValidator;
class DomainController extends Controller
{
public function __construct(private DomainValidator $validator) {}
public function validateDomain(Request $request)
{
$domain = $request->input('domain');
$isValid = $this->validator->validate($domain);
return response()->json(['valid' => $isValid]);
}
}
Key Entry Points
DomainValidator for basic domain checks (syntax, TLD, etc.).DomainParser to extract components (e.g., subdomain, TLD).config/packages/district09_domainator9k_core.yaml for custom rules.Domain Validation in Forms Use the validator in a Laravel Form Request or Symfony Validator constraint:
use District09\Domainator9kCoreBundle\Validator\Constraints\ValidDomain;
class StoreDomainRequest extends FormRequest
{
public function rules()
{
return [
'domain' => ['required', new ValidDomain()],
];
}
}
Domain Parsing for Business Logic Extract domain components (e.g., for routing or analytics):
$parser = app(DomainParser::class);
$parts = $parser->parse('sub.example.co.uk');
// Returns: ['sub' => 'sub', 'domain' => 'example', 'tld' => 'co.uk']
Custom Rules Extend the validator with custom logic (e.g., blocklist):
$validator = app(DomainValidator::class);
$validator->addRule(function ($domain) {
return !in_array($domain, ['blocked.com', 'spam.org']);
});
Integration with Eloquent Add a custom accessor to a model:
class Website extends Model
{
public function getDomainAttribute($value)
{
$validator = app(DomainValidator::class);
return $validator->validate($value) ? $value : null;
}
}
AppServiceProvider:
public function register()
{
$this->app->bind(DomainValidator::class, function ($app) {
return new \District09\Domainator9kCoreBundle\Validator\DomainValidator();
});
}
public function toArray($request)
{
return [
'domain' => $this->validator->validate($this->domain),
];
}
Case Sensitivity The validator treats domains as case-insensitive by default. Override with:
$validator->setCaseSensitive(true);
IDN (Internationalized Domain Names)
Ensure UTF-8 support is enabled in your PHP environment. The bundle may not handle IDNs (e.g., 例.测试) out of the box.
Performance Avoid parsing domains in loops without caching. Store parsed results in a temporary cache:
Cache::remember("domain_{$domain}", 3600, function() use ($parser, $domain) {
return $parser->parse($domain);
});
Deprecated Methods
Check the changelog for breaking changes (e.g., DomainValidator::check() may be renamed to validate() in future versions).
Enable Verbose Logging Configure the bundle to log validation steps:
# config/packages/district09_domainator9k_core.yaml
domainator9k_core:
debug: true
Logs will appear in storage/logs/laravel.log.
Test Edge Cases Common failure points:
example..com (double dot)example.c (single-letter TLD)example.local (pseudo-TLD)Custom Validators
Implement District09\Domainator9kCoreBundle\Validator\DomainValidatorInterface:
class CustomValidator implements DomainValidatorInterface
{
public function validate($domain): bool
{
// Custom logic
}
}
Register it as a service alias:
services:
District09\Domainator9kCoreBundle\Validator\DomainValidatorInterface: '@App\Validator\CustomValidator'
Hooks for Parsing
Extend DomainParser by subclassing and overriding parse():
class ExtendedParser extends DomainParser
{
protected function parse($domain)
{
$result = parent::parse($domain);
// Modify $result as needed
return $result;
}
}
Event Listeners
Dispatch events for domain actions (e.g., DomainValidatedEvent):
$validator->validate($domain)->then(function ($result) {
event(new DomainValidatedEvent($domain, $result));
});
domainator9k_core:
rules:
max_length: 253
allowed_chars: '[a-zA-Z0-9\-\.]'
$validator->setTldList(['com', 'org', 'custom']);
How can I help you explore Laravel packages today?