我喜欢 Laravel,但我不喜欢 ORM,我想要更快的速度……所以我使用 Lumen。但是,将我的代码移植到 Lumen 后,我发现我可以使用中间件进行一些更改......
我喜欢使用中间件通过更改将根据请求调用的控制器方法来使我的 ajax 请求更加“安静”。这是我在 Laravel 5 中所做的:
public function handle($request, Closure $next)
{
if($request->ajax() && $request->input("ajax")){
// Controller methods like: ajaxEdit, ajaxUpdate, ajaxDelete...
$ajaxMethod = "ajax".studly_case($request->input("ajax"));
// Get the route's action
$routeAction = $request->route()->getAction();
// Replace index method call with ajax method
$routeAction['uses'] = str_replace("@index", "@".$ajaxMethod, $routeAction['uses']);
$routeAction['controller'] = str_replace("@index", "@".$ajaxMethod, $routeAction['controller']);
// Update the route's action
$request->route()->setAction($routeAction);
}
// Now controller->ajaxWhatever will be called instead of controller->index
return $next($request);
}
我注意到getAction
并且setAction
在 Lumen 中不可用。我怎么能在 Lumen 中完成类似的事情?