3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
4 # Created on: 10/12/2006 03:00:32 PM PDT
8 package Catalyst::Action::REST;
13 use base 'Catalyst::Action';
22 Catalyst::Action::REST - Automated REST Method Dispatching
26 sub foo :Local :ActionClass('REST') {
27 ... do setup for HTTP method specific handlers ...
31 ... do something for GET requests ...
35 ... do somethign for PUT requests ...
40 This Action handles doing automatic method dispatching for REST requests. It
41 takes a normal Catalyst action, and changes the dispatch to append an
42 underscore and method name.
44 For example, in the synopsis above, calling GET on "/foo" would result in
45 the foo_GET method being dispatched.
47 If a method is requested that is not implemented, this action will
48 return a status 405 (Method Not Found). It will populate the "Allow" header
49 with the list of implemented request methods. You can override this behavior
50 by implementing a custom 405 handler like so:
52 sub foo_not_implemented {
53 ... handle not implemented methods ...
56 If you do not provide an _OPTIONS subroutine, we will automatically respond
57 with a 200 OK. The "Allow" header will be populated with the list of
58 implemented request methods.
60 It is likely that you really want to look at L<Catalyst::Controller::REST>,
61 which brings this class together with automatic Serialization of requests
70 This method overrides the default dispatch mechanism to the re-dispatching
71 mechanism described above.
79 my $controller = $self->class;
80 my $method = $self->name . "_" . uc( $c->request->method );
81 if ( $controller->can($method) ) {
82 $c->execute( $self->class, $self, @{ $c->req->args } );
83 return $controller->$method( $c, @{ $c->req->args } );
85 if ( $c->request->method eq "OPTIONS" ) {
86 return $self->_return_options($c);
88 my $handle_ni = $self->name . "_not_implemented";
89 if ( $controller->can($handle_ni) ) {
90 return $controller->$handle_ni( $c, @{ $c->req->args } );
92 return $self->_return_not_implemented($c);
99 my ( $self, $c ) = @_;
101 my @allowed = $self->_get_allowed_methods($c);
102 $c->response->content_type('text/plain');
103 $c->response->status(200);
104 $c->response->header( 'Allow' => \@allowed );
107 sub _get_allowed_methods {
108 my ( $self, $c ) = @_;
110 my $controller = $self->class;
111 my $methods = Class::Inspector->methods($controller);
113 foreach my $method ( @{$methods} ) {
114 my $name = $self->name;
115 if ( $method =~ /^$name\_(.+)$/ ) {
116 push( @allowed, $1 );
122 sub _return_not_implemented {
123 my ( $self, $c ) = @_;
125 my @allowed = $self->_get_allowed_methods($c);
126 $c->response->content_type('text/plain');
127 $c->response->status(405);
128 $c->response->header( 'Allow' => \@allowed );
129 $c->response->body( "Method "
130 . $c->request->method
131 . " not implemented for "
132 . $c->uri_for( $self->reverse ) );
141 You likely want to look at L<Catalyst::Controller::REST>, which implements
142 a sensible set of defaults for a controller doing REST.
144 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>
148 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
150 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
154 Daisuke Maki <daisuke@endeworks.jp>
158 You may distribute this code under the same terms as Perl itself.