- How do I install symfony/polyfill-intl-idn in a Laravel project?
- Run `composer require symfony/polyfill-intl-idn` in your Laravel project directory. The package auto-registers the `idn_to_ascii()` and `idn_to_utf8()` functions globally, requiring no additional configuration. It works alongside Laravel’s autoloader without conflicts.
- Will this package work if my server already has the PHP Intl extension enabled?
- Yes, the polyfill dynamically checks for the Intl extension at runtime. If it’s available, the native functions are used instead, ensuring zero performance penalty. This makes it safe to include even in environments where Intl is enabled.
- Can I use this polyfill with Laravel’s Str helper methods?
- Yes, you can wrap the polyfill functions in Laravel’s `Str` helper for consistency. For example, add `Str::idnToAscii($domain)` and `Str::idnToUtf8($punycode)` to your `app/Helpers/Str.php` file. This aligns with Laravel’s API style and reduces boilerplate.
- Does this support Laravel’s validator for Unicode domain validation?
- While the polyfill itself doesn’t include validation rules, you can extend Laravel’s validator with custom rules like `Rule::idnDomain()` to validate Unicode domains. Combine it with the polyfill’s conversion functions for full Unicode domain handling in forms or API requests.
- What Laravel and PHP versions does symfony/polyfill-intl-idn support?
- The package supports PHP 7.2.5 and above, with full compatibility in Laravel 5.5+. It has no breaking changes since version 1.34.0 and is regularly updated to align with Symfony’s polyfill suite. Always check the [Symfony Polyfill changelog](https://github.com/symfony/polyfill/blob/main/CHANGELOG.md) for version-specific notes.
- Is there a performance impact compared to using the native PHP Intl extension?
- Yes, the pure PHP implementation introduces a 10–30% overhead compared to the native `ext-intl`. For high-volume operations (e.g., bulk domain processing in Laravel Queues), consider caching results with Redis or benchmarking to determine if the native extension is worth enabling in your environment.
- How do I handle homograph attacks (e.g., Cyrillic ‘аpple.com’ vs. Latin ‘apple.com’) with this polyfill?
- The polyfill alone doesn’t prevent homograph attacks, as it only normalizes domains. To mitigate risks, integrate additional checks like `idn_check()` (if available) or use a third-party library like `rubix/ml` for Unicode domain validation. Log suspicious conversions via Laravel’s exception handler for review.
- Can I use this polyfill in Laravel Artisan commands or model observers?
- Absolutely. The polyfill’s functions are globally available, so you can use them directly in Artisan commands (e.g., `php artisan domain:normalize`) or model observers (e.g., `Domain::observes('creating', 'normalizeIdn')`). This enables seamless IDN handling during domain registration or migrations.
- What happens if I pass invalid Unicode input to `idn_to_ascii()` or `idn_to_utf8()`?
- The polyfill may not replicate all native Intl error codes, so invalid input will return `false`. Always check return values and log warnings using Laravel’s exception handler. For stricter validation, combine it with Laravel’s validator or a custom rule to catch edge cases early.
- Are there alternatives to symfony/polyfill-intl-idn for IDN support in Laravel?
- If you need broader Intl features beyond IDN, consider enabling the native `ext-intl` extension in your PHP environment. For minimal IDN support, alternatives like `mnapoli/idna-convert` exist, but Symfony’s polyfill is preferred for its compatibility with Laravel’s ecosystem, zero-config setup, and alignment with Symfony’s standards.