- Can I use danilovl/hashids-bundle directly in Laravel, or is it only for Symfony?
- This bundle is designed for Symfony 8+ and requires Symfony components like ParamConverter. For Laravel, you’ll need to adapt it by replacing Symfony dependencies with Laravel equivalents (e.g., middleware for route parameter decoding) or use the standalone `hashids/hashids` library directly for basic encoding/decoding.
- How do I install and configure this bundle in Laravel?
- Since this is a Symfony bundle, Laravel compatibility isn’t native. Start by installing the underlying `hashids/hashids` library via Composer (`composer require hashids/hashids`), then manually configure it in a Laravel service or middleware. For ParamConverter-like functionality, create custom middleware to decode route parameters before they reach your controllers.
- What Laravel versions does this bundle support?
- The bundle itself requires PHP 8.5+ and Symfony 8+, but the core `hashids/hashids` library works with Laravel 8.x–10.x and PHP 8.1+. You’ll need to adapt the bundle’s Symfony-specific features (like ParamConverter) to work with Laravel’s routing and middleware systems.
- How do I automatically decode Hashids in Laravel route parameters?
- Laravel doesn’t have a built-in ParamConverter, but you can achieve similar functionality with middleware. Create a middleware that decodes Hashids values from route parameters (e.g., `{id}`) and merges them into the request before passing it to your controller. This avoids manual decoding in every route.
- Is this bundle secure for obfuscating sensitive IDs like API tokens?
- Hashids is designed for obfuscation, not cryptographic security. It’s suitable for hiding numeric IDs in URLs or API tokens, but avoid using it for sensitive data like passwords or tokens requiring high security. Always use Laravel’s built-in hashing or encryption for such cases.
- What are the performance implications of using Hashids in Laravel?
- Hashids adds a small overhead (~10–20%) to encoding/decoding operations. For high-throughput APIs, benchmark the performance impact, especially if decoding occurs in middleware or route model binding. Cache frequently used Hashids instances to minimize overhead.
- How do I configure the salt, alphabet, and minimum hash length in Laravel?
- Since the bundle isn’t natively Laravel-compatible, configure these settings manually in your Laravel service or middleware. Store values in `.env` (e.g., `HASHIDS_SALT=your_salt_here`) and inject them into your Hashids instance. Example: `$hashids = new Hashids(env('HASHIDS_SALT'), 20);`.
- Are there Laravel-specific alternatives to this bundle?
- Yes, consider `spatie/laravel-hashids`, a dedicated Laravel package for Hashids integration. It provides Laravel-specific features like route model binding and easier configuration. If you need ParamConverter-like functionality, Laravel’s middleware or route model binding can replicate it without Symfony dependencies.
- How do I test Hashids encoding/decoding in Laravel?
- Test encoding/decoding in unit tests by instantiating the `Hashids` class and verifying outputs. For route parameter decoding, use Laravel’s HTTP tests to simulate requests with encoded IDs and assert the decoded values are correctly passed to controllers or middleware.
- What happens if I use a weak or default salt in production?
- A weak or default salt increases the risk of collisions or predictable ID generation. Always use a strong, unique salt (e.g., 32+ characters) and store it securely in your `.env` file. Avoid hardcoding salts in config files or version control. Rotate salts periodically if security is a concern.