Skip to content

Commit

Permalink
Merge pull request #7 from cybercog/meta/implicit-route-model-binding…
Browse files Browse the repository at this point in the history
…-readme

Readme for implicit route model binding
  • Loading branch information
Anton Komarev authored Oct 16, 2017
2 parents a146758 + 88b3134 commit 249ae2f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to `laravel-optimus` will be documented in this file.

## [2.1.0] - 2017-10-16

### Added

- Implicit route model binding ([#6](https://github.com/cybercog/laravel-optimus/pull/6))

## [2.0.0] - 2017-09-09

### Added
Expand All @@ -17,4 +23,5 @@ All notable changes to `laravel-optimus` will be documented in this file.

Initial release

[2.1.0]: https://github.com/cybercog/laravel-optimus/compare/2.0.0...2.1.0
[2.0.0]: https://github.com/cybercog/laravel-optimus/compare/1.0.0...2.0.0
65 changes: 33 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,29 @@

Laravel wrapper for the [Optimus Library](https://github.com/jenssegers/optimus) by [Jens Segers](https://github.com/jenssegers) with multiple connections support. Optimus is a small open-source library that generates short, unique, non-sequential ids from numbers. With this library, you can transform your internal id's to obfuscated integers based on Knuth's integer hash. It is similar to Hashids, but will generate integers instead of random strings. It is also super fast.

## Contents
## Contents

- [Features](#features)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Change log](#change-log)
- [Contributing](#contributing)
- [Testing](#testing)
- [Security](#security)
- [Credits](#credits)
- [Alternatives](#alternatives)
- [License](#license)
- [About CyberCog](#about-cybercog)
- [Features](#features)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Examples](#examples)
- [Change log](#change-log)
- [Contributing](#contributing)
- [Testing](#testing)
- [Security](#security)
- [Contributors](#contributors)
- [Alternatives](#alternatives)
- [License](#license)
- [About CyberCog](#about-cybercog)

## Features

- Designed to work with Laravel Eloquent models.
- Configurable multiple connections support.
- Dependency Injection ready.
- Includes Facade.
- Implicit route model binding.
- Following PHP Standard Recommendations:
- [PSR-1 (Basic Coding Standard)](http://www.php-fig.org/psr/psr-1/).
- [PSR-2 (Coding Style Guide)](http://www.php-fig.org/psr/psr-2/).
Expand Down Expand Up @@ -110,7 +112,7 @@ This is the class of most interest. It is bound to the ioc container as `optimus

This facade will dynamically pass static method calls to the `optimus` object in the ioc container which by default is the `OptimusManager` class.

#### OptimusServiceProvider
#### Providers\OptimusServiceProvider

This class contains no public methods of interest. This class should be added to the providers array in `config/app.php`. This class will setup ioc bindings.

Expand Down Expand Up @@ -166,25 +168,25 @@ use Cog\Laravel\Optimus\OptimusManager;

class Foo
{
protected $optimus;
protected $optimus;

public function __construct(OptimusManager $optimus)
{
$this->optimus = $optimus;
}

public function bar($id)
{
return $this->optimus->encode($id)
}
public function __construct(OptimusManager $optimus)
{
$this->optimus = $optimus;
}
public function bar($id)
{
return $this->optimus->encode($id)
}
}

app()->make('Foo')->bar(20);
```

#### Eloquent Model Trait
#### Implicit route model binding

To enable implicit route model binding based on the encoded ID, all you need to do is [configure the prime numbers](#optimus-numbers-generation) and use the `OptimusEncodedRouteKey` trait in your model.
To enable [implicit route model binding](https://laravel.com/docs/5.5/routing#implicit-binding) based on the encoded ID, all you need to do is [configure the prime numbers](#optimus-numbers-generation) and use the `Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey` trait in your model.

If you don't want to use the default Optimus connection, you can specify a custom connection by adding an `$optimusConnection` property to you model.

Expand All @@ -202,7 +204,7 @@ class YourModel extends Model

Now you can type hint your model in a route closure or controller and Laravel will use the encoded ID to query the database.

Note that implicit route model binding requires Laravel's `SubstituteBindings` middleware, which is part of the `web` middleware group.
*Note: Implicit route model binding requires Laravel's `Illuminate\Routing\Middleware\SubstituteBindings` middleware, which is part of the `web` middleware group.*

```php
Route::get('url/to/{model}', function (YourModel $model) {
Expand All @@ -220,7 +222,7 @@ $url = url("url/to/{$encodedId}");
Or you can use named routes and pass it the model. Laravel will do the rest.

```php
$url = route('my.named.route', [$model]);
$url = route('my.named.route', $model);
```

## Changelog
Expand All @@ -243,11 +245,10 @@ $ vendor/bin/phpunit

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Credits
## Contributors

| | @mention |
|---|---|
| ![@a-komarev](https://avatars2.githubusercontent.com/u/1849174?s=64) | [@a-komarev](https://github.com/a-komarev) |
| <a href="https://github.com/a-komarev">![@a-komarev](https://avatars.githubusercontent.com/u/1849174?s=110)<br />Anton Komarev</a> | <a href="https://github.com/ivanvermeyen">![@ivanvermeyen](https://avatars.githubusercontent.com/u/3598622?s=110)<br />Ivan Vermeyen</a> |
| :---: | :---: |

[Laravel Optimus contributors list](../../contributors)

Expand Down
4 changes: 2 additions & 2 deletions tests/Traits/OptimusEncodedRouteKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Cog\Tests\Laravel\Optimus\Stubs\Models\UserWithCustomOptimusConnection;
use Cog\Tests\Laravel\Optimus\Stubs\Models\UserWithDefaultOptimusConnection;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Route;
use Illuminate\Support\Facades\Route;

/**
* Class OptimusEncodedRouteKeyTest.
Expand Down Expand Up @@ -85,7 +85,7 @@ public function testEncodedRouteKeyIsUsedWhenGeneratingNamedRouteUrls()
})->name('test.route');

$expectedUrl = "{$this->baseUrl}/users/{$encodedId}";
$generatedUrl = route('test.route', [$user]);
$generatedUrl = route('test.route', $user);

$this->assertEquals($expectedUrl, $generatedUrl);
}
Expand Down

0 comments on commit 249ae2f

Please sign in to comment.