在 Lumen 中,如何在中间件期间更改要在路由控制器上调用的方法?
In Lumen, how do I change the method to be called on a route's controller during middleware?
我喜欢 Laravel,但我不喜欢 ORM,我想要更快的速度...所以我使用 Lumen。但是,将我的代码移植到 Lumen 后,我发现我可以使用中间件进行一些更改...
我喜欢使用中间件通过更改将根据请求调用的控制器方法来使我的 ajax 请求更多 "restful"。这是我在 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 中完成类似的事情?
现在(因为这个功能似乎是 on the road map to being added),我只是在我的路由文件的顶部处理它:
routes.php
// ***** Pre-routing logic
// Convert all ajax calls with a request parameter "ajax"
// to a corresponding controller's ajax method.
// For example, if request parameter "?ajax=edit" then
// call method SomeController@ajaxEdit.
$indexAjax = "index";
$request = $app['request'];
if($request->ajax() && $request->input("ajax")){
$indexAjax = "ajax".studly_case($request->input("ajax"));
}
// ***** Routes
$app->group(['namespace'=>'App\Http\Controllers\AdminPanel', 'prefix'=>'admin'], function($app) use ($indexAjax)
{
$app->get('login/', ['as' => 'admin-login', 'uses' => 'Login\LoginController@'.$indexAjax]);
});
$app->group(['namespace'=>'App\Http\Controllers\AdminPanel', 'middleware'=>['auth'], 'prefix'=>'admin'], function($app) use ($indexAjax)
{
$app->get('users/', ['as' => 'admin-users', 'uses' => 'Admin\UsersController@'.$indexAjax]);
$app->get('logout/', ['as' => 'admin-logout', 'uses' => 'Login\LoginController@logout']);
});
我喜欢 Laravel,但我不喜欢 ORM,我想要更快的速度...所以我使用 Lumen。但是,将我的代码移植到 Lumen 后,我发现我可以使用中间件进行一些更改...
我喜欢使用中间件通过更改将根据请求调用的控制器方法来使我的 ajax 请求更多 "restful"。这是我在 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 中完成类似的事情?
现在(因为这个功能似乎是 on the road map to being added),我只是在我的路由文件的顶部处理它:
routes.php
// ***** Pre-routing logic
// Convert all ajax calls with a request parameter "ajax"
// to a corresponding controller's ajax method.
// For example, if request parameter "?ajax=edit" then
// call method SomeController@ajaxEdit.
$indexAjax = "index";
$request = $app['request'];
if($request->ajax() && $request->input("ajax")){
$indexAjax = "ajax".studly_case($request->input("ajax"));
}
// ***** Routes
$app->group(['namespace'=>'App\Http\Controllers\AdminPanel', 'prefix'=>'admin'], function($app) use ($indexAjax)
{
$app->get('login/', ['as' => 'admin-login', 'uses' => 'Login\LoginController@'.$indexAjax]);
});
$app->group(['namespace'=>'App\Http\Controllers\AdminPanel', 'middleware'=>['auth'], 'prefix'=>'admin'], function($app) use ($indexAjax)
{
$app->get('users/', ['as' => 'admin-users', 'uses' => 'Admin\UsersController@'.$indexAjax]);
$app->get('logout/', ['as' => 'admin-logout', 'uses' => 'Login\LoginController@logout']);
});