You can use bridge in your routes:
my $r = $self->routes->bridge->to('sessions#fill_stash');
and then create routes as usual
$r->get('/page')->to('controller#action');
--
Турский Виктор
http://koorchik.blogspot.com
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To post to this group, send email to mojol...@googlegroups.com.
To unsubscribe from this group, send email to mojolicious...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mojolicious?hl=en.
For example, you have 3 routes and 3 actions
sub startup {
my $self = shift;
my $r = $self->routes;
$r->get('/user_posts')->to('user#show_posts');
$r->get('/user_messages')->to('user#show_messages');
$r->get('/user_files')->to('user#show_files');
}
for instance, in your layout you use something like this:
Global count: <%= $globalcount %>
User id: <%= $user_id %>
So, you should in every action will stash with globalcount and user_id values
$self->stash(user_id => $user_id, globalcount => $globalcount);
But this is inconvinient.
To solve this you can use bridge in next way:
sub startup {
my $self = shift;
my $r = $self->routes;
my $br = $r->bridge->to('user#fill_stash');
$br->get('/user_posts')->to('user#show_posts');
$br->get('/user_messages')->to('user#show_messages');
$br->get('/user_files')->to('user#show_files');
}
package User;
sub fill_stash {
my $self = shift;
$self->stash(user_id => $user_id, globalcount => $globalcount);
return 1;
}
...
So, fill_stash will be called before any of your actions. This allows
you to fill stash in one place.
See Mojolicious::Guides::Routing for details.
--
Turskyi Viktor
http://koorchik.blogspot.com
Виктор> 2012/1/11 ivo welch <ivo....@gmail.com>:
>>
>> hi BNKTOP(?): could you please be a little more explicit with an example?
Виктор> For example, you have 3 routes and 3 actions
Thank you so much for this example, it's short, clear and to the
point. And makes alot of sense on *why* you would use this.
Виктор> sub startup {
Виктор> my $self = shift;
Виктор> my $r = $self->routes;
Виктор> $r->get('/user_posts')->to('user#show_posts');
Виктор> $r->get('/user_messages')->to('user#show_messages');
Виктор> $r->get('/user_files')->to('user#show_files');
Виктор> }
Виктор> for instance, in your layout you use something like this:
Виктор> Global count: <%= $globalcount %>
Виктор> User id: <%= $user_id %>
Виктор> So, you should in every action will stash with globalcount and user_id values
Виктор> $self->stash(user_id => $user_id, globalcount => $globalcount);
Виктор> But this is inconvinient.
Виктор> To solve this you can use bridge in next way:
Виктор> sub startup {
Виктор> my $self = shift;
Виктор> my $r = $self->routes;
Виктор> my $br = $r->bridge->to('user#fill_stash');
Виктор> $br->get('/user_posts')->to('user#show_posts');
Виктор> $br->get('/user_messages')->to('user#show_messages');
Виктор> $br->get('/user_files')->to('user#show_files');
Виктор> }
Виктор> package User;
Виктор> sub fill_stash {
Виктор> my $self = shift;
Виктор> $self->stash(user_id => $user_id, globalcount => $globalcount);
Виктор> return 1;
Виктор> }
Виктор> ...
Виктор> So, fill_stash will be called before any of your actions. This allows
Виктор> you to fill stash in one place.
Виктор> See Mojolicious::Guides::Routing for details.
Виктор> --
Виктор> Turskyi Viktor
Виктор> http://koorchik.blogspot.com
Виктор> --
Виктор> You received this message because you are subscribed to the Google Groups "Mojolicious" group.
Виктор> To post to this group, send email to mojol...@googlegroups.com.
Виктор> To unsubscribe from this group, send email to mojolicious...@googlegroups.com.
Виктор> For more options, visit this group at http://groups.google.com/group/mojolicious?hl=en.
--