Skip to content

Commit 62925b6

Browse files
committed
Add resource missing option
1 parent 36f5aac commit 62925b6

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/Illuminate/Routing/PendingResourceRegistration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ public function shallow($shallow = true)
195195
return $this;
196196
}
197197

198+
/**
199+
* Define the callable that should be invoked on a missing model exception.
200+
*
201+
* @param $callback
202+
* @return $this
203+
*/
204+
public function missing($callback)
205+
{
206+
$this->options['missing'] = $callback;
207+
208+
return $this;
209+
}
210+
198211
/**
199212
* Indicate that the resource routes should be scoped using the given binding fields.
200213
*

src/Illuminate/Routing/ResourceRegistrar.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ class ResourceRegistrar
2020
*/
2121
protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
2222

23+
/**
24+
* Actions that use model binding.
25+
*
26+
* @var string[]
27+
*/
28+
protected $modelBoundMethods = ['show', 'edit', 'update', 'destroy'];
29+
2330
/**
2431
* The parameters set for this resource instance.
2532
*
@@ -421,6 +428,10 @@ protected function getResourceAction($resource, $controller, $method, $options)
421428
$action['where'] = $options['wheres'];
422429
}
423430

431+
if (isset($options['missing']) && in_array($method, $this->modelBoundMethods)) {
432+
$action['missing'] = $options['missing'];
433+
}
434+
424435
return $action;
425436
}
426437

tests/Routing/RouteRegistrarTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,22 @@ public function testCanRegisterResourcesWithoutOption()
334334
}
335335
}
336336

337+
public function testCanRegisterResourceWithMissingOption()
338+
{
339+
$this->router->middleware('resource-middleware')
340+
->resource('users', RouteRegistrarControllerStub::class)
341+
->missing(function () { return 'missing'; });
342+
343+
$this->assertIsCallable($this->router->getRoutes()->getByName('users.show')->getMissing());
344+
$this->assertIsCallable($this->router->getRoutes()->getByName('users.edit')->getMissing());
345+
$this->assertIsCallable($this->router->getRoutes()->getByName('users.update')->getMissing());
346+
$this->assertIsCallable($this->router->getRoutes()->getByName('users.destroy')->getMissing());
347+
348+
$this->assertNull($this->router->getRoutes()->getByName('users.index')->getMissing());
349+
$this->assertNull($this->router->getRoutes()->getByName('users.create')->getMissing());
350+
$this->assertNull($this->router->getRoutes()->getByName('users.store')->getMissing());
351+
}
352+
337353
public function testCanAccessRegisteredResourceRoutesAsRouteCollection()
338354
{
339355
$resource = $this->router->middleware('resource-middleware')

0 commit comments

Comments
 (0)