Slim PHP Route in Middleware

后端 未结 3 779
Happy的楠姐
Happy的楠姐 2020-12-28 18:41

In Slim is it possible to get the current route within middleware?

class Auth extends \\Slim\\Middleware{
  public function call(){ 
    $currentRoute = $thi         


        
3条回答
  •  悲哀的现实
    2020-12-28 18:47

    There is an alternative method of doing this, as I've been in the same situation. What I wanted to avoid was matching anything by route and wanted to use route names instead, so you could try the following:

    public function call() {
    
        $routeIWantToCheckAgainst = $this->slimApp->router()->urlFor('my.route.name');
        $requestRoute = $this->slimApp->request()->getPathInfo();
        if ($routeIWantToCheckAgainst !== $requestRoute) {
            // Do stuff you need to in here
        }
    
        $this->next->call();
    }
    

    You could even have an array of routes you DON'T want the middleware to run on and then just check if it's in_array() etc and if not, do what you need to.

提交回复
热议问题