从 Laravel 5 中的中间件获取当前路由操作名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28698600/
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
Get current route action name from middleware in laravel 5
提问by Mohamed Nagy
I have a middleware like this:
我有一个这样的中间件:
<?php
namespace App\Http\Middleware;
use App\Contracts\PermissionsHandlerInterface;
use Closure;
class PermissionsHanlderMiddleware {
public $permissionsHandler;
function __construct(PermissionsHandlerInterface $permissionsHandler) {
$this -> permissionsHandler = $permissionsHandler;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next) {
$routeAction = $request->route()->getActionName();
/*
do some operations
*/
return $next($request);
}
}
but $request->route()
always returns null
, I think its because the router hasn't been dispatched with the request.
但$request->route()
总是返回null
,我认为这是因为路由器尚未随请求一起发送。
Note:I added my middleware to Kernal.php
global middlewares to run before each request as the following
注意:我将我的中间件添加到Kernal.php
全局中间件以在每个请求之前运行,如下所示
protected $middleware = [
.
.
.
'App\Http\Middleware\PermissionsHanlderMiddleware',
];
I want to get route action name before the execution of $next($request)
to do some permission operations. How can i do this ?
我想在执行之前获取路由操作名称$next($request)
以进行一些权限操作。我怎样才能做到这一点 ?
回答by Laurence
You cannot get the route action name if the router has not yet been dispatched. The router class has not yet done its stuff - so you cannot do $router->request()
- it will just be null.
如果路由器尚未分派,则无法获取路由操作名称。路由器类还没有完成它的工作——所以你不能做$router->request()
——它只是空的。
If it runs as routeMiddleware as $routeMiddleware
- then you can just do $router->request()
如果它作为 routeMiddleware 运行$routeMiddleware
- 那么你可以这样做 $router->request()
You can get the URI string in the middleware before the router has run - and do some logic there if you like: $request->segments()
. i.e. that way you can see if the URI segment matches a specific route and run some code.
您可以在中间件得到URI字符串的路由器已经运行之前-如果你喜欢做一些逻辑有:$request->segments()
。即通过这种方式您可以查看 URI 段是否与特定路由匹配并运行一些代码。
Edit:
编辑:
One way I can quickly think of is just wrap all your routes in a group like this:
我能很快想到的一种方法是将所有路线包装在一个组中,如下所示:
$router->group(['middleware' => 'permissionsHandler'], function() use ($router) {
// Have every single route here
});
回答by B? Loong A Nh?i
This is the solution I did in my project:
这是我在我的项目中所做的解决方案:
...
public function handle($request, Closure $next) {
DB::beginTransaction();
$nextRequest = $next($request); //The router will be dispatched here, but it will reach to controller's method sometimes, so that we have to use DB transaction.
$routeName = $request->route()->getRouteName();
if ($checkPassed) {
DB::commit();
return $nextRequest;
} else {
DB::rollback();
}
}
回答by Josh Chang
This is also fine.
这也很好。
$request->path(); // path
$request->route()->getName()//name of the route