"under" literally means "under", let's take a route like this:
/post/:postid/edit
You can accomplish this as follows with 'under'
my $foo = $r->under('/post');
$foo->route('/')->to('post#index') # /foo
$foo->route('/:postid/')->to('post#view') # /foo/someid
$foo->route('/:postid/edit')->to('post#edit') # /foo/someid/bar
Using a bridge, we do this:
my $foo = $r->bridge('/post/:postid')->to('post#bridge');
$foo->route('/')->to('post#view');
$foo->route('/edit')->to('post#edit');
The major difference is this:
Using under, you must load the specified post in both Post::view and
Post::edit - now for something simple that doesn't make a big difference, but
imagine having to apply user permissions, access checks, and so on. It gets
repetitive after that.
Using a bridge, the bridge method itself is always called, so a call to
/post/1234/ will follow Post::bridge -> Post::view - a call to /post/1234/edit
will chain Post::Bridge and Post::edit together. In your bridge function you
can then access the post id, figure out where it's at, do access checks, and
depending on your return value the chain will either continue or not.
Returning 1 will continue the chain and returning 0 will terminate it.
If I'm not mistaken, "under" also affects the matching of routes, because
specifying under('/post') would mean anything under (no pun intended) that
route can be ignored if the initial fragment isn't /post - so there's use
cases for both, it depends fuly on the situation.
So, long story short, the difference between bridge and under is that the
action you specify for your bridge is called every time for anything routed
under the bridge, whereas under doesn't do this.
On 06/17/2012 01:42 PM, David Oswald wrote:
> >From Mojolicious::Routes::Route:
>
>
> bridge <
http://mojolicio.us/perldoc/Mojolicious/Routes/Route#toc>
> my $bridge = $r->bridge;
> my $bridge = $r->bridge('/:action');
> my $bridge = $r->bridge('/:action', action => qr/\w+/);
> my $bridge = $r->bridge(format => 0);
>
> Generate bridge.
>
>
> .....
>
>
> under <
http://mojolicio.us/perldoc/Mojolicious/Routes/Route#toc>
> my $route = $r->under(sub {...});
> my $route = $r->under('/:foo');
> Generate bridge. ...
> I guess in the context of a full app I'm unclear as to what is the
> difference. I think that 'under' is a specialized form of bridge. Are they
> mostly interchangeable?
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/mojolicious/-/k3zAb3r8Rc0J.
> 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.
--
Ben van Staveren
phone: +62 81 70777529
email:
benvans...@gmail.com