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