#Hashids for Laravel 3
This bundle uses the classes made by hashids.org
Generate hashes from numbers, like YouTube or Bitly. Use hashids when you do not want to expose your database ids to the user.
Hashids has two classes, one for PHP 5.4 and the other one for PHP 5.3. You won't need to worry about which file is loaded, because the bundle checks which PHP is running and then uses a class suitable for your server.
First you need to create a salt and/or length of the hashes. These values can be changed in the start.php in the bundle directory.
$config = array(
'salt' => 'Place your salt here',
'length' => 6
);
To autoload the hashids bundle you'll need to add it to your application/bundles.php array.
return array(
// ....other array items....
'hashids' => array('auto' => true),
// ....other array items....
);
Now you are ready to use this beast of a bundle!
Because hashids is registered in the IoC (Inverse of Control) container we don't worry about passing any parameters, this is all taken care of cia Dependecy Injection
$hasher = IoC::resolve('hashids');
You can either encrypt one id...
$hash = $hasher->encrypt(1); // Creating hash... zi7Bib
...or multiple...
$hash = $hasher->encrypt(1, 21, 12, 12, 666); // Creating hash... MMtaUpSGhdA
Same thing but then the other way around...
$hash = $hasher->decrypt('zi7Bib');
// Returns
array (size=1)
0 => int 1
...or multiple...
$hash = $hasher->decrypt('MMtaUpSGhdA');
// Returns
array (size=5)
0 => int 1
1 => int 21
2 => int 12
3 => int 12
4 => int 666
If you don't want to create an instance and just keep it all on one line that is possible too.
Encrypt:
$hash = IoC::resolve('hashids')->encrypt(1); // Creating hash... zi7Bib
Decrypt
$hash = IoC::resolve('hashids')->decrypt('zi7Bib');
// Returns
array (size=1)
0 => int 1
For the documentation written by the owner of hashids, click here
Hope you will enjoy this bundle and thanks to Ivan Akimov (@ivanakimov) for making Hashids. All credits for the plugin go to him.