Why does this conditional redirect in Catalyst not work?

I have a Catalyst application and want to redirect it based on a conditional statement. I am having problems with this, and I wonder if anyone can understand why this seemingly easy task is difficult.

In my Root.pm module, I have sub begin and can be redirected to another website, for example. www.perl.org , but I can’t redirect the page to my application. Any thoughts on how to do conditional redirection?

 sub begin : Private { my ( $self, $c ) = @_; $c->stash->{client_id} = somenumber; # I'm setting this manually for testing $c->res->redirect('http://www.perl.org/') unless $c->stash->{client_id}; $c->res->redirect('http://www.mysite.com/success') if $c->stash->{client_id}; #does not } 
+4
source share
1 answer

You may be stuck in an endless loop in which your begin sub redirects the user to another page of your Catalyst application; as soon as the "controller to be started has been identified, but before the URL mappings are triggered" ( from the Catalyst::Manual::Intro page ) begin will be called again, causing another redirect, etc. d.

Try to completely remove this code from begin ; perhaps, as Htbaa suggested, auto might be what you are looking for. The standard case of $c->detach (in controller controller ):

 sub check_login :Local { # do something $c->detach('controller/login_successful') if($success); # display error message } sub login_successful :Local { # do something with the logged in user. } 

In this case, executing $c->res->redirect('http://example.com/login_successful') should work fine. Hope this helps!

+1
source

Source: https://habr.com/ru/post/1346601/


All Articles