3 # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
4 # Created on: 10/12/2006 03:00:32 PM PDT
8 package Catalyst::Action::REST;
13 use base 'Catalyst::Action';
16 use Catalyst::Request::REST;
17 use Catalyst::Controller::REST;
21 our $VERSION = '0.64';
23 # This is wrong in several ways. First, there's no guarantee that
24 # Catalyst.pm has not been subclassed. Two, there's no guarantee that
25 # the user isn't already using their request subclass.
26 Catalyst->request_class('Catalyst::Request::REST')
27 unless Catalyst->request_class->isa('Catalyst::Request::REST');
31 Catalyst::Action::REST - Automated REST Method Dispatching
35 sub foo :Local :ActionClass('REST') {
36 ... do setup for HTTP method specific handlers ...
40 ... do something for GET requests ...
44 ... do somethign for PUT requests ...
49 This Action handles doing automatic method dispatching for REST requests. It
50 takes a normal Catalyst action, and changes the dispatch to append an
51 underscore and method name.
53 For example, in the synopsis above, calling GET on "/foo" would result in
54 the foo_GET method being dispatched.
56 If a method is requested that is not implemented, this action will
57 return a status 405 (Method Not Found). It will populate the "Allow" header
58 with the list of implemented request methods. You can override this behavior
59 by implementing a custom 405 handler like so:
61 sub foo_not_implemented {
62 ... handle not implemented methods ...
65 If you do not provide an _OPTIONS subroutine, we will automatically respond
66 with a 200 OK. The "Allow" header will be populated with the list of
67 implemented request methods.
69 It is likely that you really want to look at L<Catalyst::Controller::REST>,
70 which brings this class together with automatic Serialization of requests
73 When you use this module, the request class will be changed to
74 L<Catalyst::Request::REST>.
82 This method overrides the default dispatch mechanism to the re-dispatching
83 mechanism described above.
91 my $controller = $c->component( $self->class );
92 my $method = $self->name . "_" . uc( $c->request->method );
93 if ( $controller->can($method) ) {
94 $c->execute( $self->class, $self, @{ $c->req->args } );
95 return $controller->$method( $c, @{ $c->req->args } );
97 if ( $c->request->method eq "OPTIONS" ) {
98 return $self->_return_options($c);
100 my $handle_ni = $self->name . "_not_implemented";
101 if ( $controller->can($handle_ni) ) {
102 return $controller->$handle_ni( $c, @{ $c->req->args } );
104 return $self->_return_not_implemented($c);
110 sub _return_options {
111 my ( $self, $c ) = @_;
113 my @allowed = $self->_get_allowed_methods($c);
114 $c->response->content_type('text/plain');
115 $c->response->status(200);
116 $c->response->header( 'Allow' => \@allowed );
119 sub _get_allowed_methods {
120 my ( $self, $c ) = @_;
122 my $controller = $self->class;
123 my $methods = Class::Inspector->methods($controller);
125 foreach my $method ( @{$methods} ) {
126 my $name = $self->name;
127 if ( $method =~ /^$name\_(.+)$/ ) {
128 push( @allowed, $1 );
134 sub _return_not_implemented {
135 my ( $self, $c ) = @_;
137 my @allowed = $self->_get_allowed_methods($c);
138 $c->response->content_type('text/plain');
139 $c->response->status(405);
140 $c->response->header( 'Allow' => \@allowed );
141 $c->response->body( "Method "
142 . $c->request->method
143 . " not implemented for "
144 . $c->uri_for( $self->reverse ) );
153 You likely want to look at L<Catalyst::Controller::REST>, which implements
154 a sensible set of defaults for a controller doing REST.
156 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>
160 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
162 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
166 Daisuke Maki <daisuke@endeworks.jp>
170 You may distribute this code under the same terms as Perl itself.