Skip to content

Commit e1d9840

Browse files
committed
Merge branch 'resource-missing-model' into 8.x
2 parents 65c8e0c + cf9b749 commit e1d9840

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-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 callable $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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ protected function addResourceIndex($name, $base, $controller, $options)
184184
{
185185
$uri = $this->getResourceUri($name);
186186

187+
unset($options['missing']);
188+
187189
$action = $this->getResourceAction($name, $controller, 'index', $options);
188190

189191
return $this->router->get($uri, $action);
@@ -202,6 +204,8 @@ protected function addResourceCreate($name, $base, $controller, $options)
202204
{
203205
$uri = $this->getResourceUri($name).'/'.static::$verbs['create'];
204206

207+
unset($options['missing']);
208+
205209
$action = $this->getResourceAction($name, $controller, 'create', $options);
206210

207211
return $this->router->get($uri, $action);
@@ -220,6 +224,8 @@ protected function addResourceStore($name, $base, $controller, $options)
220224
{
221225
$uri = $this->getResourceUri($name);
222226

227+
unset($options['missing']);
228+
223229
$action = $this->getResourceAction($name, $controller, 'store', $options);
224230

225231
return $this->router->post($uri, $action);
@@ -421,6 +427,10 @@ protected function getResourceAction($resource, $controller, $method, $options)
421427
$action['where'] = $options['wheres'];
422428
}
423429

430+
if (isset($options['missing'])) {
431+
$action['missing'] = $options['missing'];
432+
}
433+
424434
return $action;
425435
}
426436

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)