r31712@martha (orig r1247): groditi | 2009-10-02 17:02:01 -0400
[catagits/Reaction.git] / lib / Reaction / UI / Controller / Role / RedirectTo.pm
1 package Reaction::UI::Controller::Role::RedirectTo;
2
3 use Moose::Role;
4
5 sub redirect_to {
6   my ($self, $c, $to, $cap, $args, $attrs) = @_;
7
8   $c->log->debug(
9     "Using redirect_to is now deprecated and may be removed in the future."
10   );
11
12   #the confess calls could be changed later to $c->log ?
13   my $action;
14   my $reftype = ref($to);
15   if( $reftype eq '' ){
16     $action = $self->action_for($to);
17     confess("Failed to locate action ${to} in " . blessed($self)) unless $action;
18   } elsif($reftype eq 'ARRAY' && @$to == 2){ #is that overkill / too strict?
19     $action = $c->controller($to->[0])->action_for($to->[1]);
20     confess("Failed to locate action $to->[1] in $to->[0]" ) unless $action;
21   } elsif( blessed $to && $to->isa('Catalyst::Action') ){
22     $action = $to;
23   } else{
24     confess("Failed to locate action from ${to}");
25   }
26
27   $cap ||= $c->req->captures;
28   $args ||= $c->req->args;
29   $attrs ||= {};
30   my $uri = $c->uri_for($action, $cap, @$args, $attrs);
31   $c->res->redirect($uri);
32 }
33
34 1;
35
36 __END__;
37
38
39 =head1 NAME
40
41 Reaction::UI::Controller::Role::RedirectTo
42
43 =head1 DESCRIPTION
44
45 Provides a C<redirect_to> method, which aims to be a more convenient way to
46 create internal redirects vs C<Catalyst::uri_for> and C<Catalyst::Response::redirect>
47
48 =head1 DEPRECATION NOTICE
49
50 This method was separated out of L<Catalyst::Controller> to facilitate deprecation.
51 The behavior of this method is, by design, flawed and you should aim to replace
52 any instances of it in your codebase;
53
54 =head1 METHODS
55
56 =head2 redirect_to $c, 'action_name', \@captures, \@args, \%query_parms
57
58 =head2 redirect_to $c, $action_object, \@captures, \@args, \%query_parms
59
60 =head2 redirect_to $c, [ Controller_name => 'action_name' ], \@captures, \@args, \%query_parms
61
62 Will create a uri from the arguments given and redirect to it without detaching.
63 If captures and arguments are not explicitly given, the ones from the current
64 request will be used. If query-parameters are not given, none will be used.
65
66 The first argument after C<$c> cab be one of three, the name of an action present
67 in the controller returned by C<$c-E<gt>controller>, an action object, or an
68 array reference contraining 2 items, a controller name and an action name.
69
70 =head1 AUTHORS
71
72 See L<Reaction::Class> for authors.
73
74 =head1 LICENSE
75
76 See L<Reaction::Class> for the license.
77
78 =cut