2017 © Pedro Peláez
 

library zf2-hashids

Zend Framework 2 module for hashids allowing you to obfuscate ID's in your API

image

damess/zf2-hashids

Zend Framework 2 module for hashids allowing you to obfuscate ID's in your API

  • Tuesday, October 27, 2015
  • by dannym87
  • Repository
  • 1 Watchers
  • 0 Stars
  • 884 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 7 % Grown

The README.md

ZF2 Hashids

Build Status Coverage Status, (*1)

Full documentation

This is a Zend Framework 2 module for the PHP Hashids library. See the full documentation of the library at http://hashids.org/php., (*2)

Requirements

  • PHP 5.3.3 or higher
  • Zend Framework 2.1 onwards

(Optional) GNU Multiple Precision or BCMath to allow integers greater than 1,000,000,000 to be encoded. See https://github.com/ivanakimov/hashids.php for more information., (*3)

Installation using Composer

composer require damess/zf2-hashids

Add the module to ./config/application.config.php, (*4)

<?php

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

Options

The Hashids module has some options to allow you to quickly change the configuration. After installing the module, copy ./vendor/damess/zf2-hashids/config/hashids.global.php.dist to ./config/autoload/hashids.global.php and change the values as required., (*5)

  • salt - Default value of ''. This is the value used when encoding an ID into a hash. Note: Please do not change this value once it's been set.
  • min_length - Default value of 22. This defines the minimum length of the encoded hash value.
  • alphabet - Default value of 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'. The alphabet used when encoding an ID. This must be at least 16 characters long and not contain any spaces.
<?php

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',
    ),
);

Usage

This module provides a HashidsService and a controller plugin, see below for examples of use., (*6)

HashidsService

The HashidsService can be retrieved from the ServiceManager., (*7)

Inside a factory implementing Zend\ServiceManager\FactoryInterace

<?php

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

Inside a controller or class implementing Zend\ServiceManager\ServiceLocatorAwareInterface that's been retrieved from the ServiceManager

<?php

$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)

Controller plugin

The Hashids module provides a hashids controller plugin. This is how it can be used, (*8)

<?php

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)
        ));
    }
}

The Versions

27/10 2015

dev-master

9999999-dev

Zend Framework 2 module for hashids allowing you to obfuscate ID's in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Daniel Messenger

zf2 zend zend framework 2 hashids obfuscate

10/09 2015

v0.1.1

0.1.1.0

Zend Framework 2 module for hashids allowing you to obfuscate ID's in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Daniel Messenger

zf2 zend zend framework 2 hashids obfuscate

10/09 2015

v0.1.0

0.1.0.0

Zend Framework 2 module for hashids allowing you to obfuscate ID's in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Daniel Messenger

zf2 zend zend framework 2 hashids obfuscate