我刚刚升级到Laravel 4.1并且不能再使用我过去使用的功能了。我编写了一个函数来将传入的请求重定向到另一个路由,获取结果,并用当前的传入路由替换当前路由。我在我的前端控制器上使用它来使用我自己的API,该API在同一个应用程序中定义。
这是功能:
public static function redirectRequest($newRoute, $verb, $args = null)
{
// store the original request data and route
$originalInput = Request::input();
$originalRoute = Route::current();
$request = $args === null ? Request::create($newRoute, $verb) : Request::create($newRoute, $verb, $args);
// replace the request input for the new route...
Request::replace($request->input());
try
{
$response = Route::dispatch($request);
return $response;
}
catch (\Exception $e)
{
throw $e;
}
finally
{
// replace the request input and route back to the original state
Request::replace($originalInput);
Route::setCurrentRoute($originalRoute);
}
}
我会像以下一样使用它:
Helpers::redirectRequest('/api/v1/someroute', 'GET');
问题在于,当我尝试将内容返回到重定向之前的方式时,我不能。 setCurrentRoute
已从4.1中删除,我无法弄清楚如何重置当前路由。
答案 0 :(得分:0)
我过去做过的一件事是使用实际的routes.php来处理它。如果您将“catchall”路线注册为:
Route::get('api/v1', 'ApiController@route');
只要这是在所有其他路线(Laravel使用第一个匹配路线)之后,您就可以在ApiController中处理
public function route($uri) {
// Handle your API route using the $uri variable
}
这可能不是您正在寻找的解决方案,但我发现它非常方便。