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') {}
29 ... do something for GET requests ...
33 ... do somethign for PUT requests ...
38 This Action handles doing automatic method dispatching for REST requests. It
39 takes a normal Catalyst action, and changes the dispatch to append an
40 underscore and method name.
42 For example, in the synopsis above, calling GET on "/foo" would result in
43 the foo_GET method being dispatched.
45 If a method is requested that is not implemented, this action will
46 return a status 405 (Method Not Found). It will populate the "Allow" header
47 with the list of implemented request methods.
55 This method overrides the default dispatch mechanism to the re-dispatching
56 mechanism described above.
63 my $controller = $self->class;
64 my $method = $self->name . "_" . uc( $c->request->method );
65 if ( $controller->can($method) ) {
66 return $controller->$method($c, @{$c->req->args});
68 $self->_return_405($c);
69 return $c->execute( $self->class, $self, @{$c->req->args} );
74 my ( $self, $c ) = @_;
76 my $controller = $self->class;
77 my $methods = Class::Inspector->methods($controller);
79 foreach my $method ( @{$methods} ) {
80 my $name = $self->name;
81 if ( $method =~ /^$name\_(.+)$/ ) {
85 $c->response->content_type('text/plain');
86 $c->response->status(405);
87 $c->response->header( 'Allow' => \@allowed );
88 $c->response->body( "Method "
90 . " not implemented for "
91 . $c->uri_for( $self->reverse ) );
100 You likely want to look at L<Catalyst::Controller::REST>, which implements
101 a sensible set of defaults for a controller doing REST.
103 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>
107 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
111 You may distribute this code under the same terms as Perl itself.