Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 55 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,105 +8,97 @@ Use hashids when you do not want to expose your database ids to the user.</b>
## Installation
Begin by installing the package through Composer. Edit your project's `composer.json` file to require `mitch/hashids`.

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

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

```php
php composer.phar update
```
```bash
$ composer require mitch/hashids
```

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.

```php
'Mitch\Hashids\HashidsServiceProvider'
```
```php
'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.

```php
'Hashids' => 'Mitch\Hashids\Hashids'
```
```php
'Hashids' => 'Mitch\Hashids\Hashids'
```

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

```php
php artisan config:publish mitch/hashids
```
```php
php artisan vendor:publish
```

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

### Encoding
You can simply encrypt on id:

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

or multiple..

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

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

```php
Hashids::decode('Ri7Bi');
```php
Hashids::decode('Ri7Bi');

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

or multiple..

```php
Hashids::decode('MMtaUpSGhdA');

// Returns
array (size=5)
0 => int 1
1 => int 21
2 => int 12
3 => int 12
4 => int 666
```
```php
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..

```php
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'));
}
}
```
```php
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

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

## That's it!
Documentation about [Hashids can be found here](https://github.com/ivanakimov/hashids.php).
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.x",
"illuminate/support": "5.0.*@dev",
"illuminate/contracts": "5.0.*@dev",
"hashids/hashids": "1.0.x"
},
"autoload": {
Expand All @@ -21,7 +22,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
"dev-master": "2.x-dev"
}
}
}
12 changes: 8 additions & 4 deletions src/Mitch/Hashids/HashidsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class HashidsServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->package('mitch/hashids');
$this->publishes([
__DIR__.'/../../config/config.php' => config_path('hashids.php'),
]);
}

/**
Expand All @@ -23,16 +25,18 @@ public function boot()

public function register()
{
$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'hashids');

$this->registerHashids();
}

protected function registerHashids()
{
$this->app->bind('Hashids\Hashids', function ($app) {
return new Hashids(
$app['config']['app.key'],
$app['config']['hashids::length'],
$app['config']['hashids::alphabet']
config('app.key'),
config('hashids.length'),
config('hashids.alphabet')
);
});
}
Expand Down