Giter Club home page Giter Club logo

hashids's Introduction

Hashids for Laravel 4

This package uses the classes created by hashids.org

Generate hashes from numbers, like YouTube or Bitly. Use hashids when you do not want to expose your database ids to the user.

Installation

Begin by installing the package through Composer. Edit your project's composer.json file to require mitch/hashids.

"require": {
  "mitch/hashids": "1.x"
}

Next use Composer to update your project from the the Terminal:

php composer.phar update

Once the package has been installed you'll need to add the service provider. Open your app/config/app.php configuration file, and add a new item to the providers array.

'Mitch\Hashids\HashidsServiceProvider'

After doing this you also need to add an alias. In your app/config/app.php file, add this to the aliases array.

'Hashids' => 'Mitch\Hashids\Hashids'

Now last but not least you need to publish to package configuration from your Terminal:

php artisan config:publish mitch/hashids

Usage

Once you've followed all the steps and completed the installation you can use Hashids.

Encoding

You can simply encrypt on id:

Hashids::encode(1); // Creating hash... Ri7Bi

or multiple..

Hashids::encode(1, 21, 12, 12, 666); // Creating hash... MMtaUpSGhdA

Decoding

It's the same thing but the other way around:

Hashids::decode('Ri7Bi');

// Returns
array (size=1)
  0 => int 1

or multiple..

Hashids::decode('MMtaUpSGhdA');

// Returns
array (size=5)
  0 => int 1
  1 => int 21
  2 => int 12
  3 => int 12
  4 => int 666

Injecting Hashids

Now it's also possible to have Hashids injected into your class. Lets look at this controller as an example..

class ExampleController extends BaseController
{
    protected $hashids;

    public function __construct(Hashids\Hashids $hashids)
    {
        $this->hashids = $hashids;
    }

    public function getIndex()
    {
        $hash = $this->hashids->encode(1);
        return View::make('example.index', compact('hash'));
    }
}

The original classname and namespace has been bound in the IoC container to return our instantiated Hashids class.

Using IoC

Create a Hashids instance with the IoC

App::make('Hashids\Hashids')->encode(1);

That's it!

Documentation about Hashids can be found here.

Thanks to Ivan Akimov (@ivanakimov) for making Hashids. All credits for the Hashids package go to him.

hashids's People

Contributors

garbee avatar kostaspt avatar mitchellvanw avatar pasvaz avatar rmobis avatar tplaner avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.