CGI::Application::Plugin::DeclareREST - Declare RESTful API for CGI::Application
version 0.03
package My::App; use base 'CGI::Application'; use CGI::Application::Plugin::DeclareREST; get '/' => sub { ... }; # The main page get 'page/:id' => sub { my $self = shift; my $page_id = $self->captures->{id}; ... }; post 'page/:id' => sub { my $self = shift; my $page_id = $self->captures->{id}; ... };
This plugin brings the declarative syntax (similar to Dancer & Mojolicious::Lite) to CGI::Application. It uses Routes::Tiny to do the route-handling. It works together with default CGI::Application syntax as well as with CGI::Application::Plugin::AutoRunmode.
See Routes::Tiny add_route method.
get 'ROUTE/PATH/:some_arg' => sub { my $self = shift; # To access captured arguments: my $some_arg = $self->captures->{some_arg}; # Do what ever you'd do in regular runmode ... };
Optionally you can use constraints for the captures:
get 'article/:id', constraints => { id => qr/\d+/ } => sub { ... };
Works the same way as get, but only with HTTP POST requests.
Works the same way as get, but only with HTTP DELETE requests. Because HTTP DELETE is currently not availlable via regular html forms in todays browsers, you can tunnel it through POST with form-field _method=delete. See REST::Utils for documentation.
Works the same way as get. PUT-request can be tunneled with the same logic as delete.
Works the same way as get. PATCH-request can be tunneled with the same logic as delete.
Allows you to pass multiple http-methods as an array ref that will be handled by the same code.
any [qw( get post )] => 'product/:id' => sub { my $self = shift; my $id = $self->captures->{id}; ... };
Get the Routes::Tiny::Match object.
Returns hash-ref to captures (the arguments in the route). Same as:
my $captures_ref = $self->match->captures;
Routes::Tiny is used for the route-handling. Check out Routes::Tiny docs for the complete description of the path-syntax.
REST::Utils brings in the HTTP-method tunneling in regular html-forms. Basicly this means, that we can have hidden input named _method to tunnel delete, put, patch, etc requests.
CGI::Application
Aku Kauste <aku@cpan.org>
This software is copyright (c) 2018 by Aku Kauste.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install CGI::Application::Plugin::DeclareREST, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CGI::Application::Plugin::DeclareREST
CPAN shell
perl -MCPAN -e shell install CGI::Application::Plugin::DeclareREST
For more information on module installation, please visit the detailed CPAN module installation guide.