NAME
CGI::Application::Plugin::DeclareREST - Declare RESTful API for CGI::Application
VERSION
version 0.03
SYNOPSIS
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};
...
};
DESCRIPTION
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.
METHODS
add_route
See Routes::Tiny add_route method.
get
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 { ... };
post
Works the same way as get, but only with HTTP POST requests.
del
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.
put
Works the same way as get. PUT-request can be tunneled with the same logic as delete.
patch
Works the same way as get. PATCH-request can be tunneled with the same logic as delete.
any
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};
...
};
match
Get the Routes::Tiny::Match object.
captures
Returns hash-ref to captures (the arguments in the route). Same as:
my $captures_ref = $self->match->captures;
SEE ALSO
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.
AUTHOR
Aku Kauste <aku@cpan.org>
COPYRIGHT AND LICENSE
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.