我刚刚升级到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中删除,我不知道如何重置当前路线。
我过去所做的一件事是使用实际的routes.php来处理。如果您将“综合”路线注册为:
Route::get('api/v1', 'ApiController@route');
只要这是在所有其他路由之后(Laravel使用第一个匹配的路由),您就可以在ApiController中以如下方式处理它:>
public function route($uri) { // Handle your API route using the $uri variable }
这可能不是您要寻找的解决方案,但我发现它非常方便。