danilovl/hashids-bundle
Symfony bundle integrating Hashids for encoding/decoding IDs. Configure salt, alphabet, and minimum hash length. Optional ParamConverter/Request converter automatically decodes route/request parameters so controllers can receive entities by decoded IDs.
Installation:
composer require danilovl/hashids-bundle
Ensure Danilovl\HashidsBundle\HashidsBundle::class is registered in config/bundles.php.
Configuration:
Add basic settings in config/services.yaml:
danilovl_hashids:
salt: 'your-secret-salt-here'
min_hash_length: 12 # Adjust based on security needs
First Use Case: Encode an ID in a controller:
use Danilovl\HashidsBundle\Service\HashidsService;
class MyController {
public function __construct(private HashidsService $hashids) {}
public function show(int $id) {
$hash = $this->hashids->encode($id);
return response()->json(['hash' => $hash]);
}
}
Encoding/Decoding IDs:
// Encode
$hash = $this->hashids->encode([1, 2, 3]); // Returns "abc123xyz"
// Decode
$ids = $this->hashids->decode("abc123xyz"); // Returns [1, 2, 3]
ParamConverter Integration:
Enable in config/services.yaml:
danilovl_hashids:
enable_param_converter: true
Now route params like /user/{id:hashids} will auto-decode to integers.
Batch Processing:
$hashes = $this->hashids->encode([100, 200, 300]); // Array of hashes
$decoded = $this->hashids->decode($hashes); // Array of arrays
/product/{hash}) for obfuscation.min_hash_length.Salt Management:
parameter_bag.salt: '%env(APP_HASHIDS_SALT)%'.ParamConverter Quirks:
enable_param_converter: false during development to avoid confusion.Collision Risks:
min_hash_length (e.g., <12) increases collision probability. Monitor logs for decode failures.alphabet and salt.danilovl_hashids:
debug: true
Custom Alphabets: Override the default alphabet in config:
danilovl_hashids:
alphabet: 'custom123ABC!@#'
Event Listeners:
Extend the bundle by subscribing to hashids.encode/hashids.decode events (if supported in future versions).
Testing:
Mock HashidsService in tests:
$this->hashids->shouldReceive('encode')->andReturn('test123');
How can I help you explore Laravel packages today?