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 Route::missing() #36035

Merged
merged 7 commits into from
Jan 27, 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: 11 additions & 2 deletions src/Illuminate/Routing/Middleware/SubstituteBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class SubstituteBindings
{
Expand Down Expand Up @@ -34,9 +35,17 @@ public function __construct(Registrar $router)
*/
public function handle($request, Closure $next)
{
$this->router->substituteBindings($route = $request->route());
try {
$this->router->substituteBindings($route = $request->route());

$this->router->substituteImplicitBindings($route);
$this->router->substituteImplicitBindings($route);
} catch (ModelNotFoundException $exception) {
if ($route->getMissing()) {
return $route->getMissing()($request);
}

throw $exception;
}

return $next($request);
}
Expand Down
32 changes: 32 additions & 0 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,34 @@ public function setAction(array $action)
return $this;
}

/**
* Get the value of the missing redirect.
*
* @return \Closure|null
*/
public function getMissing()
{
$missing = $this->action['missing'] ?? null;

return is_string($missing) &&
Str::startsWith($missing, 'C:32:"Opis\\Closure\\SerializableClosure')
? unserialize($missing)
: $missing;
}

/**
* Add or change the missing redirect.
*
* @param \Closure $missing
* @return $this
*/
public function missing($missing)
{
$this->action['missing'] = $missing;

return $this;
}

/**
* Get all middleware, including the ones from the controller.
*
Expand Down Expand Up @@ -1171,6 +1199,10 @@ public function prepareForSerialization()
// throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
}

if (isset($this->action['missing']) && $this->action['missing'] instanceof Closure) {
$this->action['missing'] = serialize(new SerializableClosure($this->action['missing']));
}

$this->compileRoute();

unset($this->router, $this->container);
Expand Down
22 changes: 22 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Events\Dispatcher;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
Expand Down Expand Up @@ -1671,6 +1672,27 @@ public function testImplicitBindingsWithOptionalParameterWithExistingKeyInUri()
$this->assertSame('taylor', $router->dispatch(Request::create('foo/taylor', 'GET'))->getContent());
}

public function testImplicitBindingsWithMissingModelHandledByMissing()
{
$router = $this->getRouter();
$router->get('foo/{bar}', [
'middleware' => SubstituteBindings::class,
'uses' => function (RouteModelBindingNullStub $bar = null) {
$this->assertInstanceOf(RouteModelBindingNullStub::class, $bar);

return $bar->first();
},
])->missing(function () {
return new RedirectResponse('/', 302);
});

$request = Request::create('foo/taylor', 'GET');

$response = $router->dispatch($request);
$this->assertTrue($response->isRedirect('/'));
$this->assertEquals(302, $response->getStatusCode());
}

public function testImplicitBindingsWithOptionalParameterWithNoKeyInUri()
{
$router = $this->getRouter();
Expand Down