Slim 3在中间件中获取当前路由 [英] Slim 3 get current route in middleware
问题描述
我想在中间件类中获取当前路由的名称.以前(在Slim 2. *中),您可以像这样获取当前路线:
I want to get the name of the current I route in a middleware class. Previously (in Slim 2.*) you could fetch the current route like so:
$route = $this->app->router->getCurrentRoute();
$route = $this->app->router->getCurrentRoute();
但是此功能已在Slim的3.0版本中删除.我在Slim\App
的__invoke
方法中找到了以下代码:
But this function has been removed in the 3.0 version of Slim. I've found the following code in the __invoke
method of Slim\App
:
// Get the route info
$routeInfo = $request->getAttribute('routeInfo');
/** @var \Slim\Interfaces\RouterInterface $router */
$router = $this->container->get('router');
// If router hasn't been dispatched or the URI changed then dispatch
if (null === $routeInfo || ($routeInfo['request'] !== [$request->getMethod(), (string) $request->getUri()])) {
$request = $this->dispatchRouterAndPrepareRoute($request, $router);
$routeInfo = $request->getAttribute('routeInfo');
}
这表示当前路径在属性routeInfo
中存储为Request
.但是似乎我的自定义中间件类在属性设置之前被调用(通过$this->dispatchRouterAndPrepareRoute($request, $router);
方法).因为调用$request->getAttribute('routeInfo')
会解析为NULL
.
This indicates that the current route is stored as the attribute routeInfo
in the Request
. But it seems that my custom middleware class is called before the attribute is set (by the $this->dispatchRouterAndPrepareRoute($request, $router);
method). Because calling $request->getAttribute('routeInfo')
resolves to NULL
.
所以我的问题是;如何从中间件功能/类中获取当前路由(或路由名称)?
So my question is; how can I get the current route (or the name of the route) from a middleware function/class?
还是我应该从Slim\App
复制上面的代码?
Or should I just copy the piece of code above from Slim\App
?
推荐答案
对于Slim3,这是一个示例,向您展示如何从中间件中获取路由信息,这实际上是先前答案的组合.
For Slim3, here is an example showing you how to get routing information from within middleware, which is actually a combination of previous answers put together.
<?php
$slimSettings = array('determineRouteBeforeAppMiddleware' => true);
// This is not necessary for this answer, but very useful
if (ENVIRONMENT == "dev")
{
$slimSettings['displayErrorDetails'] = true;
}
$slimConfig = array('settings' => $slimSettings);
$app = new \Slim\App($slimConfig);
$myMiddleware = function ($request, $response, $next) {
$route = $request->getAttribute('route');
$routeName = $route->getName();
$groups = $route->getGroups();
$methods = $route->getMethods();
$arguments = $route->getArguments();
print "Route Info: " . print_r($route, true);
print "Route Name: " . print_r($routeName, true);
print "Route Groups: " . print_r($groups, true);
print "Route Methods: " . print_r($methods, true);
print "Route Arguments: " . print_r($arguments, true);
};
// Define app routes
$app->add($myMiddleware);
$app->get('/', function (\Slim\Http\Request $request, Slim\Http\Response $response, $args) {
# put some code here....
})
就我而言,我想添加中间件以确保用户在某些路由上登录,如果没有,则将其重定向到登录页面.我发现最简单的方法是在这样的路线上使用->setName()
:
In my case, I wanted to add middleware that would ensure the user was logged in on certain routes, and redirect them to the login page if they weren't. I found the easiest way to do this was to use ->setName()
on the routes like so:
$app->get('/', function (\Slim\Http\Request $request, Slim\Http\Response $response, $args) {
return $response->withRedirect('/home');
})->setName('index');
然后,如果此路由匹配,则中间件示例中的$routeName
将为"index"
.然后,我定义了不需要身份验证的路由数组列表,并检查了当前路由是否在该列表中.例如.
Then if this route was matched, the $routeName
in the middleware example will be "index"
. I then defined my array list of routes that didn't require authentication and checked if the current route was in that list. E.g.
if (!in_array($routeName, $publicRoutesArray))
{
# @TODO - check user logged in and redirect if not.
}
这篇关于Slim 3在中间件中获取当前路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!