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:
- slim's
redirect
can used within named routes; 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
Post a Comment