perl - Mojolicious dynamic route action depending on state -
I am using Mojolicious :: Plugin :: Authentication to handle authentication in my app. I am trying to establish a path for slash '/' in which there will be a controller / action if it is certified, and not otherwise (i.e., you can go to different pages, depending on whether you are certified Or not.) I'm not sure how to get acquainted with this. Here are some things I've tried to do:
$ r-> Any ('/') - & gt; From (CB = & gt; sub {my $ self = shift}; ($ self- & gt; IED_Author) {$ self-> redirect_to ('member # index');} Other {$ self-> Redirect_to ('guest # index');}});
and ...
my $ logged_in = $ r-> below (sub (my $ self = shift); if (! $ Self- & Gt; session ("username")) {return undef;} other {return 1;}}); If ($ logged_in) {$ logged_in-> Receive ('/') - to (Controller => 'Member', Action => 'Index'); } And {$ r-> Receive ('/') - & gt; To (Controller => 'Guest', Action => 'Index'); }
I do not need to use Mojolicious :: Plugin :: Authentication. I can easily set up a session token and check it for myself anyway, The problem remains: How do I create a dynamic action for a given route? Forgot to add
addendum
, I also tried:
my $ auth = $ r-> Below ('/' = & gt; sub {my $ self = shift}; # Authenticated return 1 if $ self-> is_user_assigned; # is not authenticated; return;);); $ Auth-> Find ('/') - & gt; ('Member # index'); Non-members $ r- & gt; Receive ('/') - & gt; # Routes related to ('guest # index');
Adding another extension
I just wanted to add, I can render some different renderings by state, for example For,
is_user_authenticated) {$ self-> Any ('/') - & gt; From (CB = & gt; Sub (My $ self = shift); Render ('Member / Index');} Other {$ Self-> Render ('Guest / Login');}});And that works very well though, I am still curious if there can be different controllers / verbs for a given route on the basis of state.
are the Mojo hooks that are looking for you, a very powerful feature, especially authentication Useful for:
You probably need to look in advance ()
Hope it helps.
Comments
Post a Comment