no need for OurPkgVersion with modern dzil
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / REST / ForBrowsers.pm
CommitLineData
83273f94 1package Catalyst::Action::REST::ForBrowsers;
2
3use Moose;
4use namespace::autoclean;
5
83273f94 6extends 'Catalyst::Action::REST';
7use Catalyst::Request::REST::ForBrowsers;
8
9sub BUILDARGS {
10 my $class = shift;
11 my $config = shift;
12 Catalyst::Request::REST::ForBrowsers->_insert_self_into( $config->{class} );
13 return $class->SUPER::BUILDARGS( $config, @_ );
14}
15
16override dispatch => sub {
17 my $self = shift;
18 my $c = shift;
19
20 my $req = $c->request();
21
22 return super()
23 unless $req->can('looks_like_browser')
24 && $req->looks_like_browser()
25 && uc $c->request()->method() eq 'GET';
26
27 my $controller = $c->component( $self->class );
28 my $rest_method = $self->name() . '_GET_html';
29
30 if ( $controller->action_for($rest_method)
31 || $controller->can($rest_method) ) {
32
33 return $self->_dispatch_rest_method( $c, $rest_method );
34 }
35
36 return super();
37};
38
39__PACKAGE__->meta->make_immutable;
40
411;
2a1aa89c 42
43=head1 NAME
44
45Catalyst::Action::REST::ForBrowsers - Automated REST Method Dispatching that Accommodates Browsers
46
47=head1 SYNOPSIS
48
49 sub foo :Local :ActionClass('REST::ForBrowsers') {
50 ... do setup for HTTP method specific handlers ...
51 }
52
53 sub foo_GET : Private {
54 ... do something for GET requests ...
55 }
56
57 sub foo_GET_html : Private {
58 ... do something for GET requests from browsers ...
59 }
60
61 sub foo_PUT : Private {
62 ... do something for PUT requests ...
63 }
64
65=head1 DESCRIPTION
66
67This class subclasses L<Catalyst::Action::REST> to add an additional
68dispatching hook. If the request is a GET request I<and> the request looks
69like it comes from a browser, it tries to dispatch to a C<GET_html> method
70before trying to the C<GET> method instead. All other HTTP methods are
71dispatched in the same way.
72
73For example, in the synopsis above, calling GET on "/foo" from a browser will
74end up calling the C<foo_GET_html> method. If the request is I<not> from a
75browser, it will call C<foo_GET>.
76
77See L<Catalyst::Action::REST> for more details on dispatching details.
78
79=head1 METHODS
80
81=over 4
82
83=item dispatch
84
85This method overrides the default dispatch mechanism to the re-dispatching
86mechanism described above.
87
88=back
89
90=head1 SEE ALSO
91
92You likely want to look at L<Catalyst::Controller::REST>, which implements a
93sensible set of defaults for a controller doing REST.
94
95This class automatically adds the
96L<Catalyst::TraitFor::Request::REST::ForBrowsers> role to your request class.
97
98=head1 CONTRIBUTORS
99
100Dave Rolsky E<lt>autarch@urth.orgE<gt>
101
102=head1 COPYRIGHT
103
104Copyright the above named AUTHOR and CONTRIBUTORS
105
106=head1 LICENSE
107
108You may distribute this code under the same terms as Perl itself.
109
110=cut