gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:40:32 27 4
gpt4 key购买 nike

我需要在中间件中拥有当前找到的 Controller 和操作,以便我可以进行一些身份验证。但我发现这是不可能的,因为管道就像 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 时不起作用 app/role/1 , 因为 $allRoutes只有 app/role/{id} 的索引而不是 app/role/1 .

有什么解决方法吗?

最佳答案

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

创建自定义调度程序

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

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

调用中间件(更新)

NOTE: I understand it's not elegant, since dispatch called after middleware executed, you must dispatch your dispatcher manually.



在您的中间件中,在您的 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;

PoC

关于php - 我可以使用 Lumen 在中间件中获取当前路线信息吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30577949/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com