Install via Composer:
composer require cayetanosoriano/hashids-bundle
(Note: The package is outdated; prefer videlalvaro/php-hashids + manual service registration for modern Laravel.)
Register the Bundle (Symfony 2.x context):
// app/AppKernel.php
new cayetanosoriano\HashidsBundle\cayetanosorianoHashidsBundle(),
Configure in config.yml:
cayetanosoriano_hashids:
salt: "your-secret-salt" # Required for security
min_hash_length: 8 # Default: 8
alphabet: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" # Customize if needed
First Use Case:
// Symfony 2.x Controller
$hashids = $this->get('hashids');
$hash = $hashids->encode(42); // "3k"
$decoded = $hashids->decode($hash); // [42]
Laravel Adaptation Note: Since this is a Symfony bundle, Laravel developers would:
videlalvaro/php-hashids directly (no bundle needed).config/app.php:
'hashids' => function ($app) {
return new \Hashids\Hashids('salt', 8, 'abcdefghijklmnopqrstuvwxyz');
},
app('hashids').Obfuscating IDs:
// Encode an ID (e.g., for URLs or display)
$hash = app('hashids')->encode($userId); // "aB3"
// Decode in routes or controllers
$userId = app('hashids')->decode($hash)[0];
Route Integration (Symfony 2.x):
# routing.yml
user_show:
path: /user/{hash}
defaults: { _controller: AppBundle:User:show }
requirements:
hash: \w+ # Regex to match hashids output
// Controller
public function showAction($hash) {
$id = app('hashids')->decode($hash)[0];
return $this->render(['id' => $id]);
}
Database-Friendly Hashing:
min_hash_length to balance readability vs. collision risk.Custom Alphabets:
a-z0-9 for URL safety:
alphabet: "abcdefghijklmnopqrstuvwxyz0123456789"
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton('hashids', function () {
return new \Hashids\Hashids(config('hashids.salt'), config('hashids.min_length'));
});
}
config/hashids.php):
return [
'salt' => env('HASHIDS_SALT', 'default-salt'),
'min_length' => 8,
'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
];
No Laravel Support:
videlalvaro/php-hashids for Laravel.Salt Management:
salt: "%env(HASHIDS_SALT)%"
Collision Risk:
min_hash_length (e.g., 4) increases collision odds. Test with:
$hashids->encode(range(1, 10000)); // Check for duplicates
Decode Edge Cases:
decode() returns an array (even for single IDs). Always use [0]:
$id = app('hashids')->decode($hash)[0] ?? null;
[] (not false).Performance:
php artisan config:clear && php artisan cache:clear
$hash = app('hashids')->encode(123);
var_dump(app('hashids')->decode($hash)); // Should return [123]
Custom Hashids Class:
Extend \Hashids\Hashids for project-specific logic (e.g., logging):
class CustomHashids extends \Hashids\Hashids {
public function encodeWithLog($number) {
$hash = parent::encode($number);
Log::info("Encoded $number as $hash");
return $hash;
}
}
Register in Laravel’s service provider.
Middleware for Decoding:
// Laravel 5.8+
public function handle($request, Closure $next) {
$hash = $request->route('hash');
$request->merge(['id' => app('hashids')->decode($hash)[0]]);
return $next($request);
}
Doctrine Integration (Symfony 2.x):
ParamConverter to auto-decode route parameters:
# services.yml
services:
app.hashids.param_converter:
class: cayetanosoriano\HashidsBundle\Doctrine\ParamConverter\HashidsParamConverter
tags:
- { name: doctrine.paramconverter }
How can I help you explore Laravel packages today?