如何在中间件 Laravel 中获取请求的控制器和动作的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30442746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get name of requested controller and action in middleware Laravel
提问by Deejay
I am new to Laravel, and i want to get name of requested controller and action in beforefilter middelware.
我是 Laravel 的新手,我想在 beforefilter 中间件中获取请求的控制器和操作的名称。
Thanks, DJ
谢谢,DJ
回答by Limon Monte
Laravel 5.6:
Laravel 5.6:
class_basename(Route::current()->controller);
Laravel 5.5 and lower:
Laravel 5.5 及更低版本:
You can retrieve the current action name with Route::currentRouteAction()
. Unfortunately, this method will return a fully namespaced class name. So you will get something like:
您可以使用 检索当前操作名称Route::currentRouteAction()
。不幸的是,此方法将返回一个完全命名空间的类名。所以你会得到类似的东西:
App\Http\Controllers\FooBarController@method
Then just separate method name and controller name:
然后只需将方法名称和控制器名称分开:
$currentAction = \Route::currentRouteAction();
list($controller, $method) = explode('@', $currentAction);
// $controller now is "App\Http\Controllers\FooBarController"
$controller = preg_replace('/.*\\/', '', $controller);
// $controller now is "FooBarController"