我可以使用 Lumen 在中间件中获取当前路线信息吗?

Jas*_*onW 4 php laravel lumen

我需要在中间件中拥有当前找到的控制器和操作,以便我可以进行一些身份验证。但我发现这是不可能的,因为管道就像 Middleware1 -> Middleware2-> do the dispatching -> controller@action() -> Middleware2 -> Middleware1。

因此,在调度之前,我无法获取路线信息。在 $controller->action() 之后再做肯定是不对的。

我做了一些研究,发现了这一点。

$allRoutes = $this->app->getRoutes();
$method = \Request::getMethod();
$pathInfo = \Request::getPathInfo();
$currentRoute = $allRoutes[$method.$pathInfo]['action']['uses'];
Run Code Online (Sandbox Code Playgroud)

但这在访问 URI 时不起作用app/role/1,因为$allRoutes只有索引 ofapp/role/{id}而不是app/role/1

有什么解决方法吗?

kri*_*lfa 6

经过一番研究,我得到了解决方案。他们来了:

创建自定义调度程序

首先,您必须制作自己的自定义调度程序,我的是:

App\Dispatcher\GroupCountBased
Run Code Online (Sandbox Code Playgroud)

存储为:

app/Dispatcher/GroupCountBased.php
Run Code Online (Sandbox Code Playgroud)

以下是内容GroupCountBased

<?php namespace App\Dispatcher;

use FastRoute\Dispatcher\GroupCountBased as BaseGroupCountBased;

class GroupCountBased extends BaseGroupCountBased
{
    public $current;

    protected function dispatchVariableRoute($routeData, $uri) {
        foreach ($routeData as $data) {
            if (!preg_match($data['regex'], $uri, $matches)) continue;

            list($handler, $varNames) = $data['routeMap'][count($matches)];

            $vars = [];
            $i = 0;

            foreach ($varNames as $varName) {
                $vars[$varName] = $matches[++$i];
            }

            // HERE WE SET OUR CURRENT ROUTE INFORMATION
            $this->current = [
                'handler' => $handler,
                'args' => $vars,
            ];

            return [self::FOUND, $handler, $vars];
        }

        return [self::NOT_FOUND];
    }
}
Run Code Online (Sandbox Code Playgroud)

在 Laravel 容器中注册您的自定义调度程序

然后,通过方法注册您自己的自定义调度程序singleton()注册所有路线执行此操作!就我而言,我bootstrap/app.php在此行之后添加了自定义调度程序:

require __DIR__.'/../app/Http/routes.php';
Run Code Online (Sandbox Code Playgroud)

这是它的样子:

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

require __DIR__.'/../app/Http/routes.php';

// REGISTER YOUR CUSTOM DISPATCHER IN LARAVEL CONTAINER VIA SINGLETON METHOD
$app->singleton('dispatcher', function () use ($app) {
    return FastRoute\simpleDispatcher(function ($r) use ($app) {
        foreach ($app->getRoutes() as $route) {
            $r->addRoute($route['method'], $route['uri'], $route['action']);
        }
    }, [
        'dispatcher' => 'App\\Dispatcher\\GroupCountBased',
    ]);
});

// SET YOUR CUSTOM DISPATCHER IN APPLICATION CONTEXT
$app->setDispatcher($app['dispatcher']);
Run Code Online (Sandbox Code Playgroud)

调用中间件(更新)

注意:我知道这并不优雅,因为dispatch在执行中间件后调用,您必须手动调度调度程序。

在您的中间件中,在您的handle方法中,执行以下操作:

app('dispatcher')->dispatch($request->getMethod(), $request->getPathInfo());
Run Code Online (Sandbox Code Playgroud)

例子:

public function handle($request, Closure $next)
{
    app('dispatcher')->dispatch($request->getMethod(), $request->getPathInfo());
    dd(app('dispatcher')->current);
    return $next($request);
}
Run Code Online (Sandbox Code Playgroud)

用法

要获取您当前的路线:

app('dispatcher')->current;
Run Code Online (Sandbox Code Playgroud)

概念验证