title: Check route through Slim Middlewhere
body: <p>I have a route as follows:</p>
  
  <pre><code>$app-&gt;get('/manager/:page', $authenticate($app), function ($page) use ($app, $secretshibe) {
    $app-&gt;render('manager/pages/' . $page . '.php', ['user' =&gt; $secretshibe-&gt;get_page($page, $app-&gt;view()-&gt;getData('user'))]);
  });
  </code></pre>
  
  <p>to render users. I use <code>$authenticate</code> to check user login, which is as follows:</p>
  
  <pre><code>$authenticate = function ($app) use ($secretshibe) {
    return function () use ($app, $secretshibe) {
      if (!isset($_SESSION['user'])) {
        $_SESSION['urlRedirect'] = $app-&gt;request()-&gt;getPathInfo();
        $app-&gt;flash('error', 'Please login to see that page');
        $app-&gt;redirect('/login');
      }
    };
  };
  </code></pre>
  
  <p>What I need to do, it have another logic check which checks if the user has paid. That can be obtained from <code>$secretshibe-&gt;check_payment($username)</code> which returns a bool, so my final function looks like this.</p>
  
  <pre><code>$authenticate = function ($app) use ($secretshibe) {
    return function () use ($app, $secretshibe) {
      if (!isset($_SESSION['user'])) {
        $_SESSION['urlRedirect'] = $app-&gt;request()-&gt;getPathInfo();
        $app-&gt;flash('error', 'Please login to see that page');
        $app-&gt;redirect('/login');
      }
      if (!$secretshibe-&gt;check_payment($_SESSION['user'])) {
        $app-&gt;flash('error', 'Your account has been marked as unpaid. Please pay to continue usage.');
        $app-&gt;redirect('/manager/payment');
      }
    };
  };
  </code></pre>
  
  <p>The problem is that when it redirects to <code>/manager/payment</code> it uses the same route function, which checks authentication the same way. This results in the page getting stuck in a redirect loop. In order to stop this, I need to check if the requested route is <code>/manager/payment</code> before redirecting.</p>
  
  <p>Because the <code>$authenticate</code> function is called as middleware, it is invoked before the route function, meaning that I cannot simply pass <code>$page</code> to it. What is the best way to prevent this redirect loop?</p>