perl - Mojolicious dynamic route action depending on state -
perl - Mojolicious dynamic route action depending on state -
i using mojolicious::plugin::authentication handle authentication in app. trying set route slash '/' have 1 controller/action if authenticated, , if not (i.e., go different page depending on whether you're authenticated.) i'm not sure how go achieving this. here of things i've tried:
$r->any('/')->to(cb => sub { $self = shift; if ( $self->is_user_authenticated ) { $self->redirect_to('member#index'); } else { $self->redirect_to('guest#index'); } });
and...
my $logged_in = $r->under (sub { $self = shift; if (!$self->session("username")) { homecoming undef; } else { homecoming 1; } }); if ( $logged_in ) { $logged_in->get('/')-to(controller => 'member', action => 'index'); } else { $r->get('/')->to(controller => 'guest', action => 'index'); }
i don't have utilize mojolicious::plugin::authentication. set session token , check myself. either way, problem remains: how create dynamic action given route?
addendum
forgot add, tried this:
my $auth = $r->under('/' => sub { $self = shift; # authenticated homecoming 1 if $self->is_user_authenticated; # not authenticated homecoming undef; }); $auth->get('/')->to('member#index'); # routes related non-members $r->get('/')->to('guest#index');
adding detail
i wanted add, can render or redirect_to different based on state, e.g.,
$r->any('/')->to(cb => sub { $self = shift; if ( $self->is_user_authenticated ) { $self->render('member/index'); } else { $self->render('guest/login'); } });
and work pretty well. however, still curious if it's possible have different controllers/actions given route based on state.
mojo hooks looking for, powerful feature, particularly useful authentication:
http://mojolicio.us/perldoc/mojolicious#hooks
you need @ before_dispatch()
hope helps.
perl mojolicious
Comments
Post a Comment