# 檢索當前路由
如果你需要在應用程序中獲取當前的路由,你所需要做但就是,調用 HTTP 請求類的帶有 `'route'` 參數的 `getAttribute` 方法,它將返回當前的路由,這個路由是 `Slim\Route` 類的實例。class.
可以使用 `getName()` 獲取路由的名稱,或者使用 `getMethods()`獲取此路由支持的方法, etc。
Note: 如果你需要在 app 中間件中訪問路由,必須在配置中將 `'determineRouteBeforeAppMiddleware'` 設置為 true。否則,`getAttribute('route')` 將會返回 null。該路由在路由中間件中永遠可用。
Example:
```
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\App;
$app = new App([
'settings' => [
// Only set this if you need access to route within middleware
'determineRouteBeforeAppMiddleware' => true
]
])
// routes...
$app->add(function (Request $request, Response $response, callable $next) {
$route = $request->getAttribute('route');
$name = $route->getName();
$groups = $route->getGroups();
$methods = $route->getMethods();
$arguments = $route->getArguments();
// do something with that information
return $next($request, $response);
});
```