Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add resource missing option #36562

Merged
merged 2 commits into from
Mar 12, 2021
Merged
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
13 changes: 13 additions & 0 deletions src/Illuminate/Routing/PendingResourceRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ public function shallow($shallow = true)
return $this;
}

/**
* Define the callable that should be invoked on a missing model exception.
*
* @param $callback
* @return $this
*/
public function missing($callback)
{
$this->options['missing'] = $callback;

return $this;
}

/**
* Indicate that the resource routes should be scoped using the given binding fields.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Routing/ResourceRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ protected function addResourceIndex($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name);

unset($options['missing']);

$action = $this->getResourceAction($name, $controller, 'index', $options);

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

unset($options['missing']);

$action = $this->getResourceAction($name, $controller, 'create', $options);

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

unset($options['missing']);

$action = $this->getResourceAction($name, $controller, 'store', $options);

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

if (isset($options['missing'])) {
$action['missing'] = $options['missing'];
}

return $action;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,22 @@ public function testCanRegisterResourcesWithoutOption()
}
}

public function testCanRegisterResourceWithMissingOption()
{
$this->router->middleware('resource-middleware')
->resource('users', RouteRegistrarControllerStub::class)
->missing(function () { return 'missing'; });

$this->assertIsCallable($this->router->getRoutes()->getByName('users.show')->getMissing());
$this->assertIsCallable($this->router->getRoutes()->getByName('users.edit')->getMissing());
$this->assertIsCallable($this->router->getRoutes()->getByName('users.update')->getMissing());
$this->assertIsCallable($this->router->getRoutes()->getByName('users.destroy')->getMissing());

$this->assertNull($this->router->getRoutes()->getByName('users.index')->getMissing());
$this->assertNull($this->router->getRoutes()->getByName('users.create')->getMissing());
$this->assertNull($this->router->getRoutes()->getByName('users.store')->getMissing());
}

public function testCanAccessRegisteredResourceRoutesAsRouteCollection()
{
$resource = $this->router->middleware('resource-middleware')
Expand Down