title: Slim PHP: run routine before all routes
body: <p>Is there a way to call a routine before each route in Slim PHP? I have a RESTful API and I want to validate the login before calling the API methods. My code looks like:</p>
  
  <pre><code>$app = new Slim();
  $app-&gt;get('user/:id', function($id) use($app){
      $user = API::getUser($id);
      if($user){
          $app-&gt;response-&gt;status(200);
      }else{
          $app-&gt;response-&gt;status(404);
      }
  });
  </code></pre>
  
  <p>The API makes the request and process a JSON response. I want to attach a precondition to allow the request, something like a callback. The API has a method <code>API::validate($token)</code> that returns <code>true</code> or <code>false</code>, I want to catch this and return status code 401 if authentication fails. Some methods like <code>API::login()</code> and <code>API::register()</code> don't need this validation.</p>