Created
July 8, 2013 21:12
-
-
Save ian-kent/5952536 to your computer and use it in GitHub Desktop.
Example use case of internal forwarding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package RouteTest; | |
use Mojo::Base 'Mojolicious'; | |
# This method will run once at server start | |
sub startup { | |
my $self = shift; | |
# Documentation browser under "/perldoc" | |
$self->plugin('PODRenderer'); | |
# Router | |
my $r = $self->routes; | |
my $b1 = $r->bridge('/')->to(cb => sub { | |
shift->session('root') ? 1 : 0; | |
}); | |
my $b2 = $b1->bridge('/foo')->to(cb => sub { | |
# Make sure we have flag 'foo' | |
shift->session('foo') ? 1 : 0; | |
}); | |
my $b3 = $b2->bridge('/bar')->to(cb => sub { | |
# Make sure we have flag 'bar' | |
shift->session('bar') ? 1 : 0; | |
}); | |
# would be an action in another controller module | |
my $sub = sub { | |
my $self = shift; | |
# Sometimes qux is set | |
return if $self->session('qux'); | |
$self->render(text => "baz") | |
}; | |
$b3->get('baz')->to(cb => $sub); | |
$b3->get('qux')->to(cb => sub { | |
my $self = shift; | |
# sometimes qux is set in session | |
$self->session(qux => int(rand(2))); | |
# call baz ... would have been separate controller/action and loaded using Mojo::Loader manually | |
$sub->($self, @_); | |
# finally render something is baz didnt | |
$self->render(text => "no baz") if !$self->stash('mojo.rendered'); | |
}); | |
$r->get('/set')->to(cb => sub { | |
my $self = shift; | |
# Some session vars, would have been set after multiple authentication layers | |
$self->session('root' => 1); | |
$self->session('foo' => 1); | |
$self->session('bar' => 1); | |
$self->render(text => 'set'); | |
}); | |
$r->get('/unset')->to(cb => sub { | |
my $self = shift; | |
# Logout | |
delete $self->session->{root}; | |
delete $self->session->{foo}; | |
delete $self->session->{bar}; | |
$self->render(text => 'unset'); | |
}); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment