Mojolicious-Plugin-Restify
view release on metacpan or search on metacpan
lib/Mojolicious/Plugin/Restify.pm view on Meta::CPAN
# to set ['accounts', 'accounts/invoices'])
my $r = $self->routes;
$self->restify->routes($r, ['accounts/invoices']);
# or add the equivalent REST routes with a HASHREF (might be easier to
# visualise how collections are chained together)
my $r = $self->routes;
$self->restify->routes($r, {
accounts => {
invoices => undef
}
});
}
Next create your controller for accounts.
# Restify controller depicting the REST actions for the /accounts collection.
# (The name of the controller is the Mojo::Util::camelized version of the
# collection path.)
package MyApp::Controller::Accounts;
use Mojo::Base 'Mojolicious::Controller';
sub resource_lookup {
my $c = shift;
# To consistenly get the element's ID relative to the resource_lookup
# action, use the helper as shown below. If you need to access an element ID
# from a collection further up the chain, you can access it from the stash.
#
# The naming convention is the name of the collection appended with '_id'.
# E.g., $c->stash('accounts_id').
my $account = your_lookup_account_resource_func($c->restify->current_id);
# By stashing the $account here, it will now be available in the delete,
# read, patch, and update actions. This resource_lookup action is optional,
# but added to every collection by default to help reduce your code.
$c->stash(account => $account);
# must return a positive value to continue the dispatch chain
return 1 if $account;
# inform the end user that this specific resource does not exist
$c->reply->not_found and return 0;
}
sub create { ... }
sub delete { ... }
sub list { ... }
sub read {
my $c = shift;
# account was placed in the stash in the resource_lookup action
$c->render(json => $c->stash('account'));
}
sub patch { ... }
sub update { ... }
1;
=head1 DESCRIPTION
L<Mojolicious::Plugin::Restify> is a L<Mojolicious::Plugin>. It simplifies
generating all of the L<Mojolicious::Routes> for a typical REST I<collection>
endpoint (e.g., C</accounts> or C</invoices>) and maps the common HTTP verbs
(C<DELETE>, C<GET>, C<PATCH>, C<POST>, C<PUT>) to underlying controller class
methods.
For example, creating a I<collection> called C</accounts> would create the
routes as shown below. N.B. The C<over> option in the example below corresponds
to the name of a route condition. See L<Mojolicious::Routes/conditions>.
# The collection route shortcut below creates the following routes, and maps
# them to controllers of the camelized route's name.
#
# Pattern Methods Name Class::Method Name
# ------- ------- ---- ------------------
# /accounts * accounts
# +/ GET "accounts_list" Accounts::list
# +/ POST "accounts_create" Accounts::create
# +/:accounts_id * "accounts"
# +/ * "accounts_resource_lookup" Accounts::resource_lookup
# +/ DELETE "accounts_delete" Accounts::delete
# +/ GET "accounts_read" Accounts::read
# +/ PATCH "accounts_patch" Accounts::patch
# +/ PUT "accounts_update" Accounts::update
# expects the element id (:accounts_id) for this collection to be a uuid
my $route = $r->collection('accounts', over => 'uuid');
L<Mojolicious::Plugin::Restify> tries not to make too many assumptions, but the
author's recent experience writing a REST-based API using L<Mojolicious> has
helped shaped this plugin, and might unwittingly express some of his bias.
=head1 HELPERS
L<Mojolicious::Plugin::Restify> implements the following helpers.
=head2 restify->current_id
my $id = $c->restify->current_id;
Returns the I<element> id at the current point in the dispatch chain.
This is the only way to guarantee the correct I<element>'s resource ID in a
L<Mojolicious::Plugin::Restify> I<action>. The C<resource_lookup> I<action>,
which is added by default in both L</collection> and L</restify-routes>, is
added at different positions of the dispatch chain. As such, the router might
not have added the value of any placeholders to the
L<Mojolicious::Controller/stash> yet.
=head2 restify->routes
This helper is a wrapper around the L</collection> route shortcut. It
facilitates creating REST I<collections> using either an C<ARRAYREF> or
C<HASHREF>.
( run in 0.748 second using v1.01-cache-2.11-cpan-d8267643d1d )