2017 © Pedro Peláez
 

library public-id

Easy id obfuscation for Laravel. Powered by hashids

image

malahierba-lab/public-id

Easy id obfuscation for Laravel. Powered by hashids

  • Saturday, August 27, 2016
  • by malahierba
  • Repository
  • 1 Watchers
  • 7 Stars
  • 5,825 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 2 Open issues
  • 2 Versions
  • 17 % Grown

The README.md

Laravel Public ID

A simple and automatic short ids (like youtube ids) generator for your laravel projects. Powered by Hashids project (and malahierba dev team), (*1)

Installation

Add in your composer.json:, (*2)

{
    "require": {
        "malahierba-lab/public-id": "1.*"
    }
}

Then you need run the composer update command., (*3)

Very Important: in composer.json use the "1.*" notation, not the ">1". If in future releases we change the conversion tool, you can loose your old public id values. For prevent this, always stay in the same mayor version., (*4)

Use

After Public ID installation, you must add the trait in any model which you want get public id functionality., (*5)

Example for Post Model:, (*6)

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Malahierba\PublicId\PublicId;

class Post extends Model {
    use PublicId;

    //your code...

After that, you can setup the static variables to define the settings for the publicID: public_id_salt, public_id_min_length, public_id_alphabet, in previous example:, (*7)

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Malahierba\PublicId\PublicId;

class Post extends Model {
    use PublicId;

    static protected $public_id_salt = 'some_string_for_salt_your_ids';

    static protected $public_id_min_length = 6; // min length for your generated short ids.

    static protected $public_id_alphabet = 'ABCDEFGHIJKLM'; // Only letters A-M

    //your code...

There are 4 predefined alphabets you can use. Setting the public_id_alphabet variable to upper_alphanumeric, upper_alpha, lower_alphanumeric, lower_alpha will use those predefined alphabets:, (*8)

upper_alphanumeric => ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
upper_alpha => ABCDEFGHIJKLMNOPQRSTUVWXYZ
lower_alphanumeric => abcdefghijklmnopqrstuvwxyz0123456789
lower_alpha => abcdefghijklmnopqrstuvwxyz

That's all. Now you can use the Public ID functionality in your Post Model:, (*9)

Get the Public ID (a.k.a. short id) for a model:, (*10)

$short_id =  $post->public_id;

Get a model based on a public id string, (*11)

$post = Post::findByPublicId($public_id);

Get the original ID with the public id string, (*12)

$original_id = Post::publicIdDecode($public_id);

Big numbers

The max ID than can be managed by default is a billion (1.000.000.000). But, depending of your environment this number could be bigger. For test the max ID in your environment setup you can use the testPublicIdMaxInt function:, (*13)

$max_id = Post::testPublicIdMaxInt();

Licence

This project has MIT licence. For more information please read LICENCE file., (*14)

The Versions

27/08 2016

dev-master

9999999-dev

Easy id obfuscation for Laravel. Powered by hashids

  Sources   Download

MIT

The Requires

 

laravel hashids public id short id obfuscate id id obfuscator

17/08 2015

1.0.1

1.0.1.0

Easy id obfuscation for Laravel. Powered by hashids

  Sources   Download

MIT

The Requires

 

laravel hashids public id short id obfuscate id id obfuscator