bridge, under.... clarification?

337 views
Skip to first unread message

David Oswald

unread,
Jun 17, 2012, 2:42:40 AM6/17/12
to mojol...@googlegroups.com
From Mojolicious::Routes::Route:


bridge
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
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?

Ben van Staveren

unread,
Jun 17, 2012, 3:24:17 AM6/17/12
to mojol...@googlegroups.com
"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

Sebastian Riedel

unread,
Jun 17, 2012, 4:03:40 AM6/17/12
to mojol...@googlegroups.com
> 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.


I'm afraid this is not the case, bridge and under are exactly the same (aside from accepting slightly different arguments). Suggestions for how to make this more clear in the documentation would be very welcome.

--
Sebastian Riedel
http://twitter.com/kraih
http://mojolicio.us



Ben van Staveren

unread,
Jun 17, 2012, 4:52:42 AM6/17/12
to mojol...@googlegroups.com
Wups!

Okay because that's what I took away from the documentation in the sense that
under and bridge are two distinctly different beasts. I'll dig through it some
more, see if I can get you some doc patches later :)
Reply all
Reply to author
Forward
0 new messages