Skip to content

Commit

Permalink
use an easier example for rewriting in cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 8, 2013
1 parent cd67b8c commit efcf40b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,4 +1,6 @@

4.13 2013-06-08

4.12 2013-06-07
- Improved Mojo::Message::Request to allow a few more ASCII characters in
URLs.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -41,7 +41,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Top Hat';
our $VERSION = '4.12';
our $VERSION = '4.13';

sub AUTOLOAD {
my $self = shift;
Expand Down
7 changes: 4 additions & 3 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -252,13 +252,14 @@ you can use a C<before_dispatch> hook to rewrite incoming requests.
});

Since reverse proxies generally don't pass along information about path
prefixes your application might be deployed under, rewriting the base path of
prefixes your application might be deployed under, rewriting the base URL of
incoming requests is also quite common.

# Move first part from path to base path in production mode
# Change base URL in production mode
app->hook(before_dispatch => sub {
my $self = shift;
push @{$self->req->url->base->path}, shift @{$self->req->url->path};
$self->req->url->base(Mojo::URL->new('http://example.com/rebased/'));
$self->req->url->path->leading_slash(0);
}) if app->mode eq 'production';

=head2 Application embedding
Expand Down
10 changes: 9 additions & 1 deletion t/mojolicious/rebased_lite_app.t
Expand Up @@ -13,7 +13,9 @@ use Test::Mojo;
# Rebase hook
app->hook(
before_dispatch => sub {
shift->req->url->base(Mojo::URL->new('http://example.com/rebased/'));
my $self = shift;
$self->req->url->base(Mojo::URL->new('http://example.com/rebased/'));
$self->req->url->path->leading_slash(0);
}
);

Expand Down Expand Up @@ -45,6 +47,7 @@ $t->get_ok('/')->status_is(200)->header_is('X-Route' => 'root')
http://example.com/rebased/
<script src="/rebased/mojo/jquery/jquery.js"></script>
<img src="/rebased/images/test.png" />
http://example.com/rebased
http://example.com/rebased/foo
/rebased/foo
http://example.com/
Expand All @@ -58,6 +61,7 @@ $t->get_ok('/foo')->status_is(200)->header_is('X-Route' => 'foo')
http://example.com/rebased/
<link href="/rebased/b.css" media="test" rel="stylesheet" />
<img alt="Test" src="/rebased/images/test.png" />
http://example.com/rebased/foo
http://example.com/rebased
/rebased
http://example.com/
Expand All @@ -76,6 +80,7 @@ $t->get_ok('/foo')->status_is(200)->content_is(<<EOF);
http://example.com/rebased/works!too!
<link href="/rebased/b.css" media="test" rel="stylesheet" />
<img alt="Test" src="/rebased/images/test.png" />
http://example.com/rebased/foo
http://example.com/rebased
/rebased
http://example.com/
Expand All @@ -88,6 +93,7 @@ $t->get_ok('/baz')->status_is(200)->header_is('X-Route' => 'baz')
http://example.com/rebased/
<script src="/rebased/mojo/jquery/jquery.js"></script>
<img src="/rebased/images/test.png" />
http://example.com/rebased/baz
http://example.com/rebased/foo
/rebased/foo
http://example.com/
Expand All @@ -104,6 +110,7 @@ __DATA__
%= $self->req->url->base
%= javascript '/mojo/jquery/jquery.js'
%= image '/images/test.png'
%= $self->req->url->to_abs
%= url_for('foo')->to_abs
%= url_for 'foo'
%= url_for('foo')->base
Expand All @@ -116,6 +123,7 @@ __DATA__
<%= $self->req->url->base %><%= flash 'just' || '' %><%= flash 'works' || '' %>
%= stylesheet '/b.css', media => 'test'
%= image '/images/test.png', alt => 'Test'
%= $self->req->url->to_abs
%= url_for('root')->to_abs
%= url_for 'root'
%= url_for('root')->base
Expand Down

0 comments on commit efcf40b

Please sign in to comment.