Hashids Component for CakePHP
PHP
Latest commit fb0125e Mar 12, 2015 @stgogm stgogm Update README.md
Fixed a typo
Permalink
Failed to load latest commit information.
HashidsComponent.php
README.md

README.md

cakephp-hashids

Hashids Component for CakePHP 2.x

Usage

First, get Hashids.php and HashGenerator.php from http://www.hashids.org/php/ and put them in your app's Vendor/Hashids folder.

Then put HashidsComponent.php to your app's Controller/Component folder.

And lastly, add this to your AppController.php:

public $components = array (
    // ...
    'Hashids' => array (
        'salt' => 'YOUR_SALT_HERE', // Required
        'min_hash_length' => 0, // Optional
        'alphabet' => '', // Optional
    )
);

Encoding

Use in your Controllers like this:

$encoded = $this->Hashids->encode(1, 2, 3);

Or in your Components, Views or anywhere else:

$encoded = HashidsComponent::encode(1, 2, 3);

Decoding

By default, this component returns the first element of the resulting decode:

$this->Hashids->decode($encoded); // --> 1

If you wish to get an array instead, set the second parameter to true, like this:

$this->Hashids->decode($encoded, true); // --> array(1, 2, 3);

Update

Since v1.0.0, several public functions of Hashids were renamed to be more appropriate:

  • Function encrypt() changed to encode()
  • Function decrypt() changed to decode()
  • Function encrypt_hex() changed to encode_hex()
  • Function decrypt_hex() changed to decode_hex()

The component's method names were also changed to maintain consistency.