php - What is the correct way to redirect a request in middleware? -


i trying implement (in)famous improved persistent session middleware in slim microframework.

there places in algorithm described application should check user's cookie , redirect user if cookie has expired or invalid. unfortunately, impossible redirect user within middleware, 2 reasons:

  1. slim's redirect can used within named routes;
  2. redirect create entirely new request, restarting slim application. same conditions triggered redirect before re-triggered, creating infinite loop.

problem 1 can solved clever use of hooks, not sure problem 2. notice some middleware solves using custom exception, catch slim's error handler, , call redirect:

// handle possible 403 middleware can throw $app->error(function (\exception $e) use ($app) {     ...     if ($e instanceof httpunauthorizedexception) {         return $app->redirectto('login');     }     ... }); 

but not best way it. there other ways can accomplish this?

what listed above fine way of doing it, , how it's done. assuming login page doesn't check httpunauthorizedexcepion, there no way ever redirect loop.


Comments

Popular posts from this blog

c# - How can I have the VS Debugger break in the correct location for exceptions thrown in Async methods in a Console App? -

c# - Why Does .NET 4.6 Specific Code Compile When Targeting Older Versions of the Framework? -

php - Issues outputting data after prepared statement with loops -