如何在中间件Laravel中获取请求的控制器和动作的名称 [英] How to get name of requested controller and action in middleware Laravel

查看:829
本文介绍了如何在中间件Laravel中获取请求的控制器和动作的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手,我想在beforefilter middelware中获取请求的控制器和操作的名称.

I am new to Laravel, and i want to get name of requested controller and action in beforefilter middelware.

谢谢, DJ

推荐答案

Laravel 5.6:

class_basename(Route::current()->controller);

Laravel 5.5及更低版本:

您可以使用Route::currentRouteAction()检索当前操作名称.不幸的是,该方法将返回一个完全命名空间的类名.这样您将得到类似的信息:

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:

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"

这篇关于如何在中间件Laravel中获取请求的控制器和动作的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆