laravel 如何在流明框架中获取中间件的当前路线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32818477/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:24:51  来源:igfitidea点击:

How to get the current route on middleware in lumen framework?

phplaravelpermissionsmiddlewarelumen

提问by Yo_win

I have develop the API application using lumen. And for the access permission control. I want to get the current route in middleware. But, I always get null on:

我已经使用 lumen 开发了 API 应用程序。并用于访问权限控制。我想在中间件中获取当前路由。但是,我总是得到空:

   $route = $request->route();

I already try the way on the Can I get current route information in middleware with Lumen?which using the routeMiddleware and dispatcher. But it's still return null. How could I get the current route on middleware?

我已经尝试过我可以使用 Lumen 在中间件中获取当前路由信息吗?其中使用了路由中间件和调度程序。但它仍然返回null。我怎样才能获得中间件的当前路线?

Thank a lot..

非常感谢..

回答by Playnox

Please update your Lumen... Everything works with no issues

请更新您的流明...一切正常

namespace App\Http\Middleware;

public function handle($request, Closure $next)
{
    $route = $request->route();
    $path = $request->getPathInfo();

    // your code here
    return $next($request);
}

回答by user4743036

maybe this

$request   = new \Illuminate\Http\Request;
$method    = $request->getMethod();
$pathInfo  = app()->getPathInfo();
$routeName = app()->getRoutes()[$method.$pathInfo]['action']['as'];

回答by Razvan Grigore

There is a more elegant way of doing this.

有一种更优雅的方法可以做到这一点。

Extend the Application class if you have not already and add this extra method:

如果您还没有扩展 Application 类并添加这个额外的方法:

use Laravel\Lumen\Application;

class YourApplication extends Application
{
    /** override other methods if needed */

    /**
     * @return string
     */
    public function getCurrentRoute()
    {
        return $this->currentRoute;
    }

}

}

Then you can access it in your Middleware like this:

然后你可以像这样在你的中间件中访问它:

$route = app()->getCurrentRoute();
$action = $route[1];
$info = $action['uses']; // string(57) "YourApp\Http\Controller\Public\UserController@view"

回答by Greedying Wang

in global middleware, you can not get route from request directly, but you can get if in a routeMiddleware.

在全局中间件中,您不能直接从请求中获取路由,但可以在路由中间件中获取。

so, just use routeMiddleware, and in middleware, $request->route().

因此,只需使用routeMiddleware, 并在中间件中使用$request->route().

if you just want to get it in a global middleware, just clone the $request, and set a dispatcher

如果你只想在全局中间件中获取它,只需克隆 $request,并设置一个调度程序

回答by Webidew

Not actually. Lumen is using a totally different router and NOT native laravel router. So it is not the same. Although lumen was based on laravel, some (or should I say almost 60% of the engine was different. Including the router app, which uses Nikic/fastroute....

实际上不。Lumen 使用的是完全不同的路由器,而不是原生的 La​​ravel 路由器。所以是不一样的。虽然 lumen 是基于 laravel 的,但有些(或者我应该说几乎 60% 的引擎是不同的。包括使用 Nikic/fastroute 的路由器应用程序......

回答by Lud Akell

$uri = $request->path();

From the Lumen docs: https://lumen.laravel.com/docs/5.8/requests

来自 Lumen 文档:https: //lumen.laravel.com/docs/5.8/requests

回答by Andranik Badalyan

Create a middleware

创建中间件

<?php

namespace App\Http\Middleware;

use Closure;

class RouteNameMiddleware
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $request->attributes->set('currentRouteName', array_search($request->getPathInfo(), app()->router->namedRoutes));

        // your code here
        return $next($request);
    }
}

Then you can access your attributes in Controller / Blade View

然后你可以在控制器/刀片视图中访问你的属性

app('request')->get('currentRouteName')

回答by radmen

Unfortunately it's not possible. At least it's not as simple as calling getCurrentRoute().

不幸的是,这是不可能的。至少它不像调用getCurrentRoute().

You need to go through routes collection and match them again with request path.

您需要通过路由收集并再次将它们与请求路径匹配。

Take a look at this rough example: https://gist.github.com/radmen/92200c62b633320b98a8

看看这个粗略的例子:https: //gist.github.com/radmen/92200c62b633320b98a8

Please note that some parts of this code may not work ;) I've extracted this code from my app (slightly different use case) and tried to fit it to your case.

请注意,此代码的某些部分可能无法正常工作;) 我已从我的应用程序中提取了此代码(用例略有不同),并尝试使其适合您的情况。

回答by cre8

Route::currentRouteName();

will return you the name of the route;

将返回路线名称;

回答by Flyingkiwi9

From the Laravel docs:

来自 Laravel 文档:

http://laravel.com/docs/5.1/requests#basic-request-information

http://laravel.com/docs/5.1/requests#basic-request-information

The path method returns the request's URI. So, if the incoming request is targeted at http://domain.com/foo/bar, the path method will return foo/bar:

path 方法返回请求的 URI。因此,如果传入请求的目标是http://domain.com/foo/bar,则路径方法将返回 foo/bar:

$uri = $request->path();

There are even other methods you may find helpful:

甚至还有其他方法可能对您有帮助:

if ($request->is('admin/*')) {
    // do something
}