How to add middileware to slim 2

82 views Asked by At

Thanks for reading my problem,

I am trying to add middleware to slim framework but it encounter with an error

Uncaught InvalidArgumentException: All Route middleware must be callable

           $authenticateForRole = function ( $user_Id, $tokenKey ) {
                 try {
                     // my stuffs 
                      }
                  } catch (\Throwable $th) {
                      echo $th->getMessage();
                      return false;
                  }
              };


              $app->map('/averageresponsetime/',$authenticateForRole($UserId, $token), function () use ($app) { 
                   echo json_encode($post1);
              })->via('POST');
              $app->run();
1

There are 1 answers

0
tahabas On

i think you should use a clousure as the documentation states;

$authenticateForRole = function ( $user_Id, $tokenKey ) {
    return function () use ( $user_Id, $tokenKey ) {
                 try {
                     // my stuffs 
                      }
                  } catch (\Throwable $th) {
                      echo $th->getMessage();
                      return false;
                  }
        }
};