How to change route in the before hook?
Using Dancer2 I would like to change the route before the user reaches the route. (e.g. if the user is not logged in) https://metacpan.org/pod/Dancer2::Cookbook#Before-filters---processed-before... seems to suggest forward hook before => sub { forward '/foo/oversee'; }; but that just lead me to deep recursion. Is this a bug or am I doing something wrong? My assumption was that after that forward the before hook should not run again. So maybe I need to rephrase the question? How to change the rout on the fly and NOT call the before hook again? regards Gabor
On Thu, 24 Oct 2013 10:16:54 +0200 Gabor Szabo <gabor@szabgab.com> wrote:
Using Dancer2 I would like to change the route before the user reaches the route. (e.g. if the user is not logged in)
https://metacpan.org/pod/Dancer2::Cookbook#Before-filters---processed-before...
seems to suggest forward
hook before => sub { forward '/foo/oversee'; };
but that just lead me to deep recursion.
Is this a bug or am I doing something wrong?
My assumption was that after that forward the before hook should not run again. So maybe I need to rephrase the question? How to change the rout on the fly and NOT call the before hook again?
My understanding is that forward essentially starts a new request internally, in much the same way as an external redirect would have but without an additional HTTP exchange; as such the hook runs again. A suitable fix would be to check the requested path and only forward if the request isn't for the acceptable path, e.g. hook before => sub { if (request->path ne '/foo/oversee') { forward '/foo/oversee'; } }; -- David Precious ("bigpresh") <davidp@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github
participants (2)
-
David Precious -
Gabor Szabo