From 66057c3c1af63001383cd73e83c5a8adc94cef97 Mon Sep 17 00:00:00 2001 From: groditi Date: Fri, 2 Oct 2009 21:01:17 +0000 Subject: move redirect_to to a role and deprecate it --- lib/Reaction/UI/Controller/Role/RedirectTo.pm | 78 +++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 lib/Reaction/UI/Controller/Role/RedirectTo.pm (limited to 'lib/Reaction/UI/Controller/Role/RedirectTo.pm') diff --git a/lib/Reaction/UI/Controller/Role/RedirectTo.pm b/lib/Reaction/UI/Controller/Role/RedirectTo.pm new file mode 100644 index 0000000..7d35b10 --- /dev/null +++ b/lib/Reaction/UI/Controller/Role/RedirectTo.pm @@ -0,0 +1,78 @@ +package Reaction::UI::Controller::Role::RedirectTo; + +use Moose::Role; + +sub redirect_to { + my ($self, $c, $to, $cap, $args, $attrs) = @_; + + $c->log->debug( + "Using redirect_to is now deprecated and may be removed in the future." + ); + + #the confess calls could be changed later to $c->log ? + my $action; + my $reftype = ref($to); + if( $reftype eq '' ){ + $action = $self->action_for($to); + confess("Failed to locate action ${to} in " . blessed($self)) unless $action; + } elsif($reftype eq 'ARRAY' && @$to == 2){ #is that overkill / too strict? + $action = $c->controller($to->[0])->action_for($to->[1]); + confess("Failed to locate action $to->[1] in $to->[0]" ) unless $action; + } elsif( blessed $to && $to->isa('Catalyst::Action') ){ + $action = $to; + } else{ + confess("Failed to locate action from ${to}"); + } + + $cap ||= $c->req->captures; + $args ||= $c->req->args; + $attrs ||= {}; + my $uri = $c->uri_for($action, $cap, @$args, $attrs); + $c->res->redirect($uri); +} + +1; + +__END__; + + +=head1 NAME + +Reaction::UI::Controller::Role::RedirectTo + +=head1 DESCRIPTION + +Provides a C method, which aims to be a more convenient way to +create internal redirects vs C and C + +=head1 DEPRECATION NOTICE + +This method was separated out of L to facilitate deprecation. +The behavior of this method is, by design, flawed and you should aim to replace +any instances of it in your codebase; + +=head1 METHODS + +=head2 redirect_to $c, 'action_name', \@captures, \@args, \%query_parms + +=head2 redirect_to $c, $action_object, \@captures, \@args, \%query_parms + +=head2 redirect_to $c, [ Controller_name => 'action_name' ], \@captures, \@args, \%query_parms + +Will create a uri from the arguments given and redirect to it without detaching. +If captures and arguments are not explicitly given, the ones from the current +request will be used. If query-parameters are not given, none will be used. + +The first argument after C<$c> cab be one of three, the name of an action present +in the controller returned by C<$c-Econtroller>, an action object, or an +array reference contraining 2 items, a controller name and an action name. + +=head1 AUTHORS + +See L for authors. + +=head1 LICENSE + +See L for the license. + +=cut -- cgit v1.2.3-54-g00ecf