【问题标题】:How to get the current route on middleware in lumen framework?如何在 lumen 框架中获取中间件的当前路由?
【发布时间】:2015-12-25 10:17:21
【问题描述】:

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

   $route = $request->route();

我已经在Can I get current route information in middleware with Lumen? 上尝试了使用 routeMiddleware 和调度程序的方式。但它仍然返回null。如何在中间件上获取当前路由?

非常感谢..

【问题讨论】:

    标签: php laravel permissions middleware lumen


    【解决方案1】:

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

    namespace App\Http\Middleware;
    
    public function handle($request, Closure $next)
    {
        $route = $request->route();
        $path = $request->getPathInfo();
    
        // your code here
        return $next($request);
    }
    

    【讨论】:

    • 请注意,->route() 将是 null,除非您使用的是 named 路由。
    【解决方案2】:
    maybe this
    
    $request   = new \Illuminate\Http\Request;
    $method    = $request->getMethod();
    $pathInfo  = app()->getPathInfo();
    $routeName = app()->getRoutes()[$method.$pathInfo]['action']['as'];
    

    【讨论】:

    • 解释每一行的作用会很有帮助
    【解决方案3】:

    很遗憾,这是不可能的。至少不像调用getCurrentRoute()那么简单。

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

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

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

    【讨论】:

      【解决方案4】:

      有一种更优雅的方法。

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

      use Laravel\Lumen\Application;
      
      class YourApplication extends Application
      {
          /** override other methods if needed */
      
          /**
           * @return string
           */
          public function getCurrentRoute()
          {
              return $this->currentRoute;
          }
      

      }

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

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

      【讨论】:

        【解决方案5】:

        来自 Laravel 文档:

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

        path 方法返回请求的 URI。所以,如果传入的请求是针对http://domain.com/foo/bar,那么path方法会返回foo/bar:

        $uri = $request->path();
        

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

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

        【讨论】:

        • 这只是一个流明问题而不是 laravel 问题
        • @BillGarrison Lumen 基于 laravel。这应该适用于 laravel。
        【解决方案6】:

        在全局中间件中,不能直接从request中获取路由,但是可以在routeMiddleware中获取。

        所以,只需使用routeMiddleware,在中间件中使用$request->route()

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

        【讨论】:

          【解决方案7】:

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

          【讨论】:

          • 这是对主题的更好评论,而不是问题的解决方案
          【解决方案8】:
          $uri = $request->path();
          

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

          【讨论】:

            【解决方案9】:

            创建中间件

            <?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);
                }
            }
            

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

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

            【讨论】:

              猜你喜欢
              • 2018-02-10
              • 2015-08-15
              • 2016-12-16
              • 2016-04-01
              • 2016-04-08
              • 2018-06-30
              • 2018-03-11
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多