PHP code example of damess / zf2-hashids

1. Go to this page and download the library: Download damess/zf2-hashids library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

damess / zf2-hashids example snippets




return array(
    'modules' => array(
        'Application',
        'DaMess\Hashids',
    ),
    ...
);



return array(
    'hashids' => array(
    
        /*
         * The salt to use for encryption
         * NOTE: Do not change this once it's been set
         */
        'salt'       => '',
        
        /*
         * Minimum length of the generated hash
         */
        'min_length' => 22,
        
        /*
         * Define which characters are used when building the hash
         */
        'alphabet'   => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
    ),
);



$service = $serviceLocator->get('DaMess\Hashids\Service\HashidsService');
$service->encode(1); // 39J4q2VolejRejNmGQBW71 (assuming default config values)
$service->decode('39J4q2VolejRejNmGQBW71'); // array(1) (assuming default config values)



$service = $this->getServiceLocator()->get('DaMess\Hashids\Service\HashidsService');
$service->encode(1); // 39J4q2VolejRejNmGQBW71 (assuming default config values)
$service->decode('39J4q2VolejRejNmGQBW71'); // array(1) (assuming default config values)



namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel(array(
            'hash' => $this->hashids()->encode(1), // 39J4q2VolejRejNmGQBW71
            'id'   => $this->hashids()->decode('39J4q2VolejRejNmGQBW71'), // array(1)
        ));
    }
}