From: John Napiorkowski Date: Tue, 25 Nov 2014 21:13:10 +0000 (-0600) Subject: allow the redirect method to accept a URI style object to reduce boilerplate for... X-Git-Tag: 5.90079_001~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=00038a21d88ab4f3620068c7b15e8f02c1b13e2d allow the redirect method to accept a URI style object to reduce boilerplate for common cases --- diff --git a/Changes b/Changes index 231624d..0165821 100644 --- a/Changes +++ b/Changes @@ -18,6 +18,8 @@ path or pathparts in your actions. query and body parameter keys are now also subject to utf8 decoding (or as specificed via the encoding configuration value). - lots of UTF8 changes. Again we think this is now more correct but please test. + - Allow $c->res->redirect($url) to accept $url as an object that does ->as_string + which I think will ease a common case (and common bug) and added documentation. 5.90077 - 2014-11-18 - We store the PSGI $env in Catalyst::Engine for backcompat reasons. Changed diff --git a/lib/Catalyst/Response.pm b/lib/Catalyst/Response.pm index 709f0ad..7368a66 100644 --- a/lib/Catalyst/Response.pm +++ b/lib/Catalyst/Response.pm @@ -351,6 +351,12 @@ qualified (= C, etc.) or that starts with a slash thing and is not a standard behaviour. You may opt to use uri_for() or uri_for_action() instead. +B If $url is an object that does ->as_string (such as L, which is +what you get from ->uri_for) we automatically call that to stringify. This +should ease the common case usage + + return $c->res->redirect( $c->uri_for(...)); + =cut sub redirect { @@ -360,6 +366,10 @@ sub redirect { my $location = shift; my $status = shift || 302; + if(blessed($location) && $location->can('as_string')) { + $location = $location->as_string; + } + $self->location($location); $self->status($status); } diff --git a/t/utf_incoming.t b/t/utf_incoming.t index 096ba69..920a064 100644 --- a/t/utf_incoming.t +++ b/t/utf_incoming.t @@ -172,6 +172,11 @@ use Encode 2.21 'decode_utf8', 'encode_utf8'; is decode_utf8($res->content), "$url", 'correct body'; #should do nothing is $res->content, "$url", 'correct body'; is $res->content_length, 90, 'correct length'; + + # Test to make sure redirect can now take an object (sorry don't have a better place for it + # but wanted test coverage. + my $location = $c->res->redirect( $c->uri_for($c->controller('Root')->action_for('uri_for')) ); + ok !ref $location; } done_testing;