OX::RouteBuilder::REST - OX::RouteBuilder which routes to an action method in a controller class based on HTTP verbs
version 0.006
package MyApp;
use OX;
use OX::RouteBuilder::REST;
has thing => (
is => 'ro',
isa => 'MyApp::Controller::Thing',
);
router as {
route '/thing' => 'REST.thing.root';
route '/thing/:id' => 'REST.thing.item';
};
package MyApp::Controller::Thing;
use Moose;
sub root_GET {
my ($self, $req) = @_;
... # return a list if things
}
sub root_PUT {
my ($self, $req) = @_;
... # create a new thing
}
sub item_GET {
my ($self, $req, $id) = @_;
... # view a thing
}
sub item_POST {
my ($self, $req, $id) = @_;
... # update a thing
}
This is an OX::RouteBuilder which routes to an action method in a controller class based on HTTP verbs. It's a bit of a mixture between OX::RouteBuilder::ControllerAction and OX::RouteBuilder::HTTPMethod.
To enable this RouteBuilder, you need to use OX::RouteBuilder::REST
in your main application class.
The action_spec
should be a string in the form
"REST.$controller.$action"
, where $controller
is the name of a
service which provides a controller instance. For each HTTP verb you
want to support you will need to set up an action with the name
$action_$verb
(e.g. $action_GET
, $action_PUT
, etc). If no
matching action-verb-method is found, a 501 error will be returned.
controller
and action
will also be automatically added as
defaults for the route, as well as name
(which will be set to
"REST.$controller.$action"
).
A HEAD
request will be redirect to GET
(with a potential
response body removed), unless you implement a method named
$action_HEAD
.
To generate a link to an action, use uri_for
with either the name
(eg "REST.$controller.$action"
), or by passing a HashRef <{ controller =
$controller, action => $action }>>. See t/test.t
for some examples.
- Thomas Klausner domm@plix.at
- Validad GmbH http://validad.com
This software is copyright (c) 2014 - 2023 by Thomas Klausner.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.