Slim 3 get current route in middleware

后端 未结 5 1926
故里飘歌
故里飘歌 2021-01-04 13:30

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->ap

5条回答
  •  孤城傲影
    2021-01-04 14:17

    Here's how you get current route in your middleware in Slim framework 3:

    $routeInfo = $request->getAttribute('routeInfo');
    

    Note that you should use this inside __invoke() function in your middleware. Here's the sample usage:

    public function __invoke($request, $response, $next)
        {
            ....
            $routeInfo = $request->getAttribute('routeInfo');
            ....
        }
    

    $routeInfo shall then contain an object like:

    {
        "0": 1,
        "1": "route6",
        "2": {
          "name": "loremipsum"
        },
        "request": [
          "POST",
          "http://example.org/loremipsum"
        ]
      }
    

提交回复
热议问题