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

[5.5] Ability to build a response using a fallback route name #21299

Merged
merged 3 commits into from
Sep 21, 2017
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
48 changes: 34 additions & 14 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,20 +572,7 @@ public function dispatch(Request $request)
*/
public function dispatchToRoute(Request $request)
{
// First we will find a route that matches this request. We will also set the
// route resolver on the request so middlewares assigned to the route will
// receive access to this route instance for checking of the parameters.
$route = $this->findRoute($request);

$request->setRouteResolver(function () use ($route) {
return $route;
});

$this->events->dispatch(new Events\RouteMatched($route, $request));

$response = $this->runRouteWithinStack($route, $request);

return $this->prepareResponse($request, $response);
return $this->buildResponse($request, $this->findRoute($request));
}

/**
Expand All @@ -603,6 +590,26 @@ protected function findRoute($request)
return $route;
}

/**
* Return the response for the given route.
*
* @param Route $route
* @param Request $request
* @return mixed
*/
protected function buildResponse(Request $request, Route $route)
{
$request->setRouteResolver(function () use ($route) {
return $route;
});

$this->events->dispatch(new Events\RouteMatched($route, $request));

return $this->prepareResponse($request,
$this->runRouteWithinStack($route, $request)
);
}

/**
* Run the given route within a Stack "onion" instance.
*
Expand Down Expand Up @@ -1102,6 +1109,19 @@ public function auth()
$this->post('password/reset', 'Auth\ResetPasswordController@reset');
}

/**
* Return a response out of the given route.
*
* @param string $name
* @return mixed
*/
public function respondWith($name)
{
return $this->buildResponse($this->currentRequest,
tap($this->routes->getByName($name))->bind($this->currentRequest)
);
}

/**
* Set the unmapped global resource parameters to singular.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Integration/Routing/FallbackRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ public function test_no_routes()
$this->assertEquals(404, $this->get('/non-existing')->getStatusCode());
}

public function test_respond_with_named_fallback_route()
{
Route::fallback(function () {
return response('fallback', 404);
})->name('testFallbackRoute');

Route::get('one', function () {
return Route::respondWith('testFallbackRoute');
});

$this->assertContains('fallback', $this->get('/non-existing')->getContent());
$this->assertContains('fallback', $this->get('/one')->getContent());
}

public function test_no_fallbacks()
{
Route::get('one', function () {
Expand Down