HTTP method matching for actions This page is obsolete. Use this instead.
You can define a Moose::Role using Catalyst::Controller::ActionRole to specify HTTP methods for Catalyst actions.
package MyApp::ActionRole::Methods; use Moose::Role; use Perl6::Junction qw/ any /; around match => sub { my $orig = shift; my $self = shift; my ( $c ) = @_; # Continue to action if no HTTP methods specified $self->$orig( @_ ) and return 0 if ! $self->attributes->{Methods}; # Get HTTP methods specified by Methods() attribute my @methods = split( /\s/, $self->attributes->{Methods}->[0] ); return 0 if ! @methods; # Continue to action if current HTTP method is specified if( $c->request->method eq any( @methods ) ) { $self->$orig( @_ ); } }; 1;
Then, in your controller, extend Catalyst::Controller::ActionRole:
package MyApp::Controller::Bar; use Moose; BEGIN { extends "Catalyst::Controller::ActionRole"; }; # Package-wide config alleviates the need to have a # Does( "Methods" ) attribute defined for every action. # See Catalyst::Controller::ActionRole for details. __PACKAGE__->config( # ~ will append "MyApp::ActionRole::" to the specified attribute action_roles => [ "~Methods" ], ); # Action requiring a GET sub index : Path Methods(" GET" ) { } # Action requiring either PUT or POST sub update : Path Methods( "PUT POST" ) { } # If you want all HTTP methods allowed, # leave out the Methods attribute sub foo : Path { }
Showing changes from previous revision. Removed | Added

