├── tests └── .gitkeep ├── .gitignore ├── src ├── config │ └── config.php └── Mitch │ └── Hashids │ ├── Hashids.php │ └── HashidsServiceProvider.php ├── .travis.yml ├── phpunit.xml ├── composer.json └── README.md /tests/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | 4, 5 | 'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 6 | ); -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | 7 | before_script: 8 | - curl -s http://getcomposer.org/installer | php 9 | - php composer.phar install --dev 10 | 11 | script: phpunit -------------------------------------------------------------------------------- /src/Mitch/Hashids/Hashids.php: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mitch/hashids", 3 | "version": "1.0", 4 | "description": "Laravel package for Hashids", 5 | "authors": [ 6 | { 7 | "name": "Mitchell van Wijngaarden", 8 | "email": "mitchell@kooding.nl" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=5.3.0", 13 | "illuminate/support": "4.x", 14 | "hashids/hashids": "1.0.x" 15 | }, 16 | "autoload": { 17 | "psr-0": { 18 | "Mitch\\Hashids": "src/" 19 | } 20 | }, 21 | "minimum-stability": "dev", 22 | "extra": { 23 | "branch-alias": { 24 | "dev-master": "1.x-dev" 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Mitch/Hashids/HashidsServiceProvider.php: -------------------------------------------------------------------------------- 1 | package('mitch/hashids'); 16 | } 17 | 18 | /** 19 | * Register the service provider. 20 | * 21 | * @return void 22 | */ 23 | 24 | public function register() 25 | { 26 | $this->registerHashids(); 27 | } 28 | 29 | protected function registerHashids() 30 | { 31 | $this->app->bind('Hashids\Hashids', function ($app) { 32 | return new Hashids( 33 | $app['config']['app.key'], 34 | $app['config']['hashids::length'], 35 | $app['config']['hashids::alphabet'] 36 | ); 37 | }); 38 | } 39 | 40 | /** 41 | * Get the services provided by the provider. 42 | * 43 | * @return array 44 | */ 45 | public function provides() 46 | { 47 | return array('Hashids\Hashids'); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hashids for Laravel 4 2 | 3 | This package uses the classes created by [hashids.org](http://www.hashids.org/ "http://www.hashids.org/") 4 | 5 | Generate hashes from numbers, like YouTube or Bitly. 6 | Use hashids when you do not want to expose your database ids to the user. 7 | 8 | ## Installation 9 | Begin by installing the package through Composer. Edit your project's `composer.json` file to require `mitch/hashids`. 10 | 11 | ```php 12 | "require": { 13 | "mitch/hashids": "1.x" 14 | } 15 | ``` 16 | 17 | Next use Composer to update your project from the the Terminal: 18 | 19 | ```php 20 | php composer.phar update 21 | ``` 22 | 23 | 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. 24 | 25 | ```php 26 | 'Mitch\Hashids\HashidsServiceProvider' 27 | ``` 28 | 29 | After doing this you also need to add an alias. In your `app/config/app.php` file, add this to the `aliases` array. 30 | 31 | ```php 32 | 'Hashids' => 'Mitch\Hashids\Hashids' 33 | ``` 34 | 35 | Now last but not least you need to publish to package configuration from your Terminal: 36 | 37 | ```php 38 | php artisan config:publish mitch/hashids 39 | ``` 40 | 41 | ## Usage 42 | Once you've followed all the steps and completed the installation you can use Hashids. 43 | 44 | ### Encoding 45 | You can simply encrypt on id: 46 | 47 | ```php 48 | Hashids::encode(1); // Creating hash... Ri7Bi 49 | ``` 50 | 51 | or multiple.. 52 | 53 | ```php 54 | Hashids::encode(1, 21, 12, 12, 666); // Creating hash... MMtaUpSGhdA 55 | ``` 56 | 57 | ### Decoding 58 | It's the same thing but the other way around: 59 | 60 | ```php 61 | Hashids::decode('Ri7Bi'); 62 | 63 | // Returns 64 | array (size=1) 65 | 0 => int 1 66 | ``` 67 | 68 | or multiple.. 69 | 70 | ```php 71 | Hashids::decode('MMtaUpSGhdA'); 72 | 73 | // Returns 74 | array (size=5) 75 | 0 => int 1 76 | 1 => int 21 77 | 2 => int 12 78 | 3 => int 12 79 | 4 => int 666 80 | ``` 81 | ### Injecting Hashids 82 | Now it's also possible to have Hashids injected into your class. 83 | Lets look at this controller as an example.. 84 | 85 | ```php 86 | class ExampleController extends BaseController 87 | { 88 | protected $hashids; 89 | 90 | public function __construct(Hashids\Hashids $hashids) 91 | { 92 | $this->hashids = $hashids; 93 | } 94 | 95 | public function getIndex() 96 | { 97 | $hash = $this->hashids->encode(1); 98 | return View::make('example.index', compact('hash')); 99 | } 100 | } 101 | ``` 102 | The original classname and namespace has been bound in the IoC container to return our instantiated Hashids class. 103 | 104 | ### Using IoC 105 | Create a Hashids instance with the IoC 106 | 107 | ```php 108 | App::make('Hashids\Hashids')->encode(1); 109 | ``` 110 | 111 | ## That's it! 112 | Documentation about [Hashids can be found here](https://github.com/ivanakimov/hashids.php). 113 | 114 | Thanks to Ivan Akimov (@ivanakimov) for making Hashids. All credits for the Hashids package go to him. 115 | --------------------------------------------------------------------------------