uploaded 0.07 to the server, fixing path, and allowing you to pass params.
[catagits/Catalyst-Plugin-SubRequest.git] / SubRequest.pm
CommitLineData
aae30f91 1package Catalyst::Plugin::SubRequest;
2
3use strict;
4
885f6da0 5our $VERSION = '0.07';
aae30f91 6
7
8=head1 NAME
9
10Catalyst::Plugin::SubRequest - Make subrequests to actions in Catalyst
11
12=head1 SYNOPSIS
13
14 use Catalyst 'SubRequest';
15
885f6da0 16 $c->subreq('/test/foo/bar', { template => 'magic.tt' });
aae30f91 17
18=head1 DESCRIPTION
19
8c464987 20Make subrequests to actions in Catalyst. Uses the catalyst
21dispatcher, so it will work like an external url call.
aae30f91 22
23=head1 METHODS
24
25=over 4
26
885f6da0 27=item subreq path, [stash as hash ref], [parameters as hash ref]
aae30f91 28
29=item sub_request
30
8c464987 31Takes a full path to a path you'd like to dispatch to. Any additional
32parameters are put into the stash.
aae30f91 33
34=back
35
36=cut
37
38*subreq = \&sub_request;
39
8c464987 40use Data::Dumper qw/Dumper/;
aae30f91 41sub sub_request {
885f6da0 42 my ( $c, $path, $stash, $params ) = @_;
8c464987 43
aae30f91 44 my %old_req;
1774feac 45 $path =~ s/^\///;
d4125755 46 local $c->{stash}=$stash || {};
aae30f91 47 $old_req{content} = $c->res->output;$c->res->output(undef);
1774feac 48 $old_req{args} = $c->req->arguments;
49 $old_req{action} = $c->req->action;$c->req->action(undef);
885f6da0 50 $old_req{path} = $c->req->path;$c->req->path($path);
51 $old_req{params} = $c->req->params;$c->req->params($params || {});
1774feac 52 $c->prepare_action();
885f6da0 53 $c->log->debug("Subrequest to $path , action is ". $c->req->action )
54 if $c->debug;
aae30f91 55 $c->dispatch();
56 my $output = $c->res->output;
8c464987 57 $c->req->{params}=$old_req{params};
aae30f91 58 $c->req->arguments($old_req{args});
885f6da0 59 $c->req->path($old_req{path});
8c464987 60 $c->res->output($old_req{content});
aae30f91 61 return $output;
62}
63
64=head1 SEE ALSO
65
66L<Catalyst>.
67
68=head1 AUTHOR
69
70Marcus Ramberg, C<mramberg@cpan.org>
71
72=head1 THANK YOU
73
74SRI, for writing the awesome Catalyst framework
75
76=head1 COPYRIGHT
77
78This program is free software, you can redistribute it and/or modify it under
79the same terms as Perl itself.
80
81=cut
82
831;