Skip to content
This repository was archived by the owner on Jun 12, 2018. It is now read-only.

Commit 2683f35

Browse files
committed
implicit route model binding
1 parent 263a5ab commit 2683f35

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,9 @@ public function show($slug){
7272
}
7373
```
7474

75-
You can use [explicit model binding](https://laravel.com/docs/master/routing#explicit-binding) too.
75+
You can use [implicit model binding](https://laravel.com/docs/master/routing#implicit-binding) too. You don't have to do anything, it works automatically!
7676

77-
Just add this code to `RouteServiceProvider@boot`
78-
79-
```php
80-
Route::bind('post', function ($slug) {
81-
return Post::findBySlugOrFail($slug);
82-
});
83-
```
84-
85-
After that typehinted models are automatically resolved:
77+
Just typehint models and they are automatically resolved:
8678

8779
```php
8880
// app/Http/Controllers/PostController.php

src/HasHashSlug.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,34 @@ public function slug(){
7171
return $this->slug;
7272
}
7373

74+
public function getRouteKeyName(){
75+
return 'hashslug';
76+
}
77+
7478
public function getRouteKey() {
7579
return $this->slug();
7680
}
7781

82+
/**
83+
* Terrible hack to make automatic implicit route model binding possible
84+
*
85+
* @see \Illuminate\Routing\RouteBinding@forModel
86+
*
87+
* @param string|array|\Closure $column
88+
* @param string $operator
89+
* @param mixed $value
90+
* @param string $boolean
91+
* @return \Illuminate\Database\Query\Builder
92+
*/
93+
public function where(... $arguments){
94+
if($arguments[0] == 'hashslug'){
95+
$id = static::decodeSlug($arguments[1]);
96+
return parent::where($this->getKeyName(), $id);
97+
}else{
98+
return parent::where(... $arguments);
99+
}
100+
}
101+
78102
private static function decodeSlug($slug){
79103
$hashids = static::getHashids();
80104

tests/HashSlugTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,24 @@ public function alphabet_can_be_customised(){
122122

123123
/** @test */
124124
public function urls_are_generated_using_slug(){
125-
Route::resource('/posts', '\Balping\HashSlug\Tests\PostController', [
125+
Route::resource('/posts-nobind', '\Balping\HashSlug\Tests\PostControllerNoBind', [
126126
"middleware" => \Illuminate\Routing\Middleware\SubstituteBindings::class
127127
]);
128128

129129
$post = Post::forceCreate(["title" => "title1"]);
130130

131131
$this->assertEquals(
132-
'http://localhost/posts/' . $post->slug(),
133-
action('\Balping\HashSlug\Tests\PostController@show', $post)
132+
'http://localhost/posts-nobind/' . $post->slug(),
133+
action('\Balping\HashSlug\Tests\PostControllerNoBind@show', $post)
134134
);
135+
136+
$response = $this->get('/posts-nobind/' . $post->slug());
137+
138+
$this->assertEquals($post->slug(), $response->getContent());
135139
}
136140

137141
/** @test */
138142
public function route_model_binging(){
139-
Route::bind('post', function ($slug) {
140-
return Post::findBySlugOrFail($slug);
141-
});
142-
143143
Route::resource('/posts', '\Balping\HashSlug\Tests\PostController', [
144144
"middleware" => \Illuminate\Routing\Middleware\SubstituteBindings::class
145145
]);
@@ -201,4 +201,11 @@ class PostController extends Controller {
201201
public function show(Post $post){
202202
return $post->slug();
203203
}
204+
}
205+
206+
class PostControllerNoBind extends Controller {
207+
public function show($slug){
208+
$post = Post::findBySlugOrFail($slug);
209+
return $post->slug();
210+
}
204211
}

0 commit comments

Comments
 (0)