2017 © Pedro Peláez
 

library silex-hashids

Silex 2.0+ service provider for Hashids library

image

lokhman/silex-hashids

Silex 2.0+ service provider for Hashids library

  • Monday, March 6, 2017
  • by lokhman
  • Repository
  • 1 Watchers
  • 0 Stars
  • 160 Installations
  • PHP
  • 0 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

silex-hashids

StyleCI, (*1)

Hashids Service provider for Silex 2.0+ micro-framework., (*2)

This project is a part of silex-tools library., (*3)

Installation

You can install silex-hashids with Composer:, (*4)

composer require lokhman/silex-hashids

Documentation

Enable HashidsServiceProvider from Provider namespace to support brilliant Hashids library. You may setup either a single or multiple profiles to use in your application., (*5)

use Lokhman\Silex\Provider\HashidsServiceProvider;

$app->register(new HashidsServiceProvider(), [
    // default options for all profiles
    'hashids.options' => [
        'salt' => '',
        'min_length' => 0,
        'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
    ],
    // optionally set multiple profiles
    'hashids.profiles' => [
        'profile1' => [
            'min_length' => 8,
        ],
        'profile2' => [
            'salt' => 'this is my salt',
            'alphabet' => 'abcdefghijklmnopqrstuvwxyz',
        ],
    ],
]);

// single profile encoding
$hash = $app['hashids']->encode(1, 2, 3);
$app['hashids']->encode(1, 2, 3) === $hash;
$app->hashids([1, 2, 3]) === $hash;

// single profile decoding
$id = $app['hashids']->decode($hash);
$app['hashids']->decode($hash) === $id;
$app->hashids($hash) === $id;

// multiple profiles encoding
$hash = $app['hashids']['profile1']->encode(1, 2, 3);
$app['hashids:profile1']->encode(1, 2, 3) === $hash;
$app->hashids([1, 2, 3], 'profile1') === $hash;

// multiple profiles decoding
$id = $app['hashids']['profile1']->decode($hash);
$app['hashids:profile1']->decode($hash) === $id;
$app->hashids($hash, 'profile1') === $id;

N.B.: To use hashids() application method, include HashidsTrait to your Application class. This method returns FALSE on error, and if given hash contains a single encoded id, integer is returned, otherwise array., (*6)

Additionally, the service provider adds support for auto-conversion of hashed parameters in routes if you specify them with __hashids_ prefix., (*7)

// GET /users/jR (single profile)
$app->get('/users/{__hashids_id}', function(Application $app, $id) {
    return $app->json(['id' => $id]);  // { "id": 1 }
});

// GET /users/olejRejN (multiple profiles)
$app->get('/users/{__hashids_profile1_id}', function(Application $app, $id) {
    return $app->json(['id' => $id]);  // { "id": 1 }
});

License

Library is available under the MIT license. The included LICENSE file describes this in detail., (*8)

The Versions

06/03 2017

dev-master

9999999-dev

Silex 2.0+ service provider for Hashids library

  Sources   Download

MIT

The Requires

 

by Alexander Lokhman

silex tools hashids

06/03 2017

2.0.1

2.0.1.0

Silex 2.0+ service provider for Hashids library

  Sources   Download

MIT

The Requires

 

by Alexander Lokhman

silex tools hashids

24/01 2017

2.0.0

2.0.0.0

Silex 2.0+ service provider for Hashids library

  Sources   Download

MIT

The Requires

 

by Alexander Lokhman

silex tools hashids