Skip to content

Commit

Permalink
added experimental current_route helper (closes #300)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 12, 2012
1 parent dbdbad9 commit 10118ae
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 15 deletions.
5 changes: 3 additions & 2 deletions Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.60 2012-03-12 00:00:00
- Added EXPERIMENTAL current_route helper to
Mojolicious::Plugin::DefaultHelpers.
- Made text_area helper a little smarter. (sshaw, sri)
- Improved inactivity timeout logging for WebSockets.
- Fixed escaping bug in select_field and text_area helpers.
Expand Down Expand Up @@ -2309,8 +2311,7 @@ This file documents the revision history for Perl extension Mojolicious.

0.9001 2009-01-28 00:00:00
- Added proper home detection to Mojo itself. (charsbar)
- Fixed a bug where errors got cached in the routes dispatcher.
(charsbar)
- Fixed a bug where errors got cached in the router. (charsbar)
- Updated error handling in MojoX::Dispatcher::Static.
- Fixed Mojo::Message::Request::cookies() to always return a
arrayref.
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious.pm
Expand Up @@ -310,8 +310,8 @@ contain more information.
my $routes = $app->routes;
$app = $app->routes(Mojolicious::Routes->new);
The routes dispatcher, defaults to a L<Mojolicious::Routes> object. You use
this in your startup method to define the url endpoints for your application.
The router, defaults to a L<Mojolicious::Routes> object. You use this in your
startup method to define the url endpoints for your application.
sub startup {
my $self = shift;
Expand Down Expand Up @@ -460,7 +460,7 @@ application object)
=item B<before_dispatch>
Emitted right before the static and routes dispatchers start their work.
Emitted right before the static dispatcher and router start their work.
$app->hook(before_dispatch => sub {
my $c = shift;
Expand All @@ -473,7 +473,7 @@ Very useful for rewriting incoming requests and other preprocessing tasks.
=item B<after_static_dispatch>
Emitted in reverse order after the static dispatcher determined if a static
file should be served and before the routes dispatcher starts its work.
file should be served and before the router starts its work.
$app->hook(after_static_dispatch => sub {
my $c = shift;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Controller.pm
Expand Up @@ -661,7 +661,7 @@ controller, defaults to a L<Mojolicious> object.
my $m = $c->match;
$c = $c->match(Mojolicious::Routes::Match->new);
Routes dispatcher results for the current request, defaults to a
Router results for the current request, defaults to a
L<Mojolicious::Routes::Match> object.
my $name = $c->match->endpoint->name;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -146,8 +146,8 @@ Most commonly used features every L<Mojolicious> developer should know about.
The renderer can be manually started by calling the method
L<Mojolicious::Controller/"render">, but that's usually not necessary,
because it will get automatically called if nothing has been rendered after
the routes dispatcher finished its work. This also means you can have routes
pointing only to templates without actual actions.
the router finished its work. This also means you can have routes pointing
only to templates without actual actions.

$self->render;

Expand Down
11 changes: 9 additions & 2 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -425,8 +425,9 @@ Or you can just disable format detection.

=head2 Named routes

Naming your routes will allow backreferencing in many kinds of helpers
throughout the whole framework.
Naming your routes will allow backreferencing in many methods and helpers
throughout the whole framework, most of them internally rely on
L<Mojolicious::Controller/"url_for"> for this.

# /foo/abc -> {controller => 'foo', action => 'bar', name => 'abc'}
$r->route('/foo/:name')->name('test')
Expand Down Expand Up @@ -454,6 +455,12 @@ name at all.
$self->url_for('current');
$self->url_for;

To check or get the actual name of the currently active route you can also
use the helper L<Mojolicious::Plugin::DefaultHelpers/"current_route">.

# Get name for current route
my $name = $self->current_route;

=head2 HTTP methods

The method L<Mojolicious::Routes/"via"> allows only specific HTTP methods to
Expand Down
20 changes: 20 additions & 0 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -40,6 +40,16 @@ sub register {
}
);

# Add "current_route" helper
$app->helper(
current_route => sub {
my $self = shift;
return '' unless my $endpoint = $self->match->endpoint;
return $endpoint->name unless @_;
return $endpoint->name eq shift;
}
);

# Add "dumper" helper
$app->helper(
dumper => sub {
Expand Down Expand Up @@ -171,6 +181,16 @@ Append content to named buffer and retrieve it.
% end
%= content_for 'message'
=head2 C<current_route>
% if (current_route 'hello') {
Hello World!
% }
%= current_route
Check or return name of current route. Note that this helper is EXPERIMENTAL
and might change without warning!
=head2 C<dumper>
%= dumper $foo
Expand Down
44 changes: 40 additions & 4 deletions t/mojolicious/rebased_lite_app.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 9;
use Test::More tests => 19;

# "For example, if you killed your grandfather, you'd cease to exist!
# But existing is basically all I do!"
Expand All @@ -21,6 +21,14 @@ app->hook(
}
);

# Current route hook
app->hook(
after_dispatch => sub {
my $self = shift;
$self->res->headers->header('X-Route' => $self->current_route);
}
);

# GET /
get '/' => 'root';

Expand All @@ -33,32 +41,55 @@ get '/bar' => sub {
$self->redirect_to($self->url_for('foo'));
};

# GET /baz
get '/baz' => sub { shift->render('root') };

my $t = Test::Mojo->new;

# GET /
$t->get_ok('/')->status_is(200)->content_is(<<EOF);
$t->get_ok('/')->status_is(200)->header_is('X-Route' => 'root')
->content_is(<<EOF);
<base href="http://kraih.com/rebased/" />
<script src="/rebased/js/jquery.js" type="text/javascript"></script>
<img src="/rebased/images/test.png" />
http://kraih.com/rebased/foo
/rebased/foo
http://kraih.com/
root
Welcome to the root!
EOF

# GET /foo
$t->get_ok('/foo')->status_is(200)->content_is(<<EOF);
$t->get_ok('/foo')->status_is(200)->header_is('X-Route' => 'foo')
->content_is(<<EOF);
<base href="http://kraih.com/rebased/" />
<link href="/rebased/b.css" media="test" rel="stylesheet" type="text/css" />
<img alt="Test" src="/rebased/images/test.png" />
http://kraih.com/rebased
/rebased
http://kraih.com/
foo
EOF

# GET /bar
$t->get_ok('/bar')->status_is(302)
$t->get_ok('/bar')->status_is(302)->header_is('X-Route' => 'bar')
->header_is(Location => 'http://kraih.com/rebased/foo');

# GET /baz
$t->get_ok('/baz')->status_is(200)->header_is('X-Route' => 'baz')
->content_is(<<EOF);
<base href="http://kraih.com/rebased/" />
<script src="/rebased/js/jquery.js" type="text/javascript"></script>
<img src="/rebased/images/test.png" />
http://kraih.com/rebased/foo
/rebased/foo
http://kraih.com/
baz
EOF

# GET /yada (does not exist)
$t->get_ok('/yada')->status_is(404)->header_is('X-Route' => '');

__DATA__
@@ root.html.ep
%= base_tag
Expand All @@ -67,6 +98,10 @@ __DATA__
%= url_for('foo')->to_abs
%= url_for 'foo'
%= url_for('foo')->base
%= current_route
% if (current_route 'root') {
Welcome to the root!
% }
@@ foo.html.ep
%= base_tag
Expand All @@ -75,3 +110,4 @@ __DATA__
%= url_for('root')->to_abs
%= url_for 'root'
%= url_for('root')->base
%= current_route

0 comments on commit 10118ae

Please sign in to comment.