Skip to content

Commit

Permalink
Merge branch 'pr/23335' of https://github.com/themsaid/framework into…
Browse files Browse the repository at this point in the history
… themsaid-pr/23335
  • Loading branch information
taylorotwell committed Sep 21, 2017
2 parents f4b529b + 35b858b commit 5a5c598
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
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

0 comments on commit 5a5c598

Please sign in to comment.