4

我需要在中间件中有当前找到的控制器和操作,以便我可以进行一些身份验证。但我发现这是不可能的,因为管道就像 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'];

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

有什么解决方法吗?

4

3 回答 3

6

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

创建自定义调度程序

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

App\Dispatcher\GroupCountBased

存储为:

app/Dispatcher/GroupCountBased.php

以下是 的内容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];
    }
}

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

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

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

这是它的样子:

/*
|--------------------------------------------------------------------------
| 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']);

调用中间件(更新)

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

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

app('dispatcher')->dispatch($request->getMethod(), $request->getPathInfo());

例子:

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

用法

获取当前路线:

app('dispatcher')->current;

概念验证

于 2015-06-04T08:51:54.430 回答
1

我找到了这个问题的正确答案。我错过了一种名为routeMiddleware()的方法Application。此方法注册在调度后调用的特定于路由的中间件。所以只需使用$app->routeMiddleware()注册你的中间件。$request->route()并通过您的中间件获取匹配的路由信息​​。

于 2015-06-08T01:53:40.923 回答
1
$methodName = $request->getMethod();
$pathInfo = $request->getPathInfo();
$route = app()->router->getRoutes()[$methodName . $pathInfo]['action']['uses'];
$classNameAndAction = class_basename($route);
$className = explode('@', $classNameAndAction)[0];
于 2020-11-25T04:26:04.127 回答