Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use utf8;
- package Mojolicious::Plugin::CRUDRoutingHelper;
- {
- $Mojolicious::Plugin::CRUDRoutingHelper::VERSION = '0.2.1';
- }
- # ABSTRACT: routing helpers for HTTP CRUD
- use Mojo::Base 'Mojolicious::Plugin';
- use Lingua::EN::Inflect qw/PL/;
- sub register {
- my ($self, $app) = @_;
- $app->routes->add_shortcut(
- rest_routes => sub {
- my $r = shift;
- my $params = { @_ ? (ref $_[0] ? %{ $_[0] } : @_) : (0=>undef) };
- my $name = $params->{name};
- my $readonly = $params->{readonly} || 0;
- my $plural = PL($name, 10);
- $app->log->debug("Creating routes for resource '$name'");
- #
- # Generate "/$name" route, handled by controller $name
- #
- my $resource = $r->route("/$plural")->to("$name#");
- # GET requests - lists the collection of this resource
- $resource->get->to('#rest_list')->name("list_$plural");
- $app->log->info("Created route GET ".$r->to_string."/$plural (list_$plural)");
- if (!$readonly) {
- # POST requests - creates a new resource
- $resource->post->to('#rest_create')->name("create_$name");
- $app->log->info("Created route POST ".$r->to_string."/$plural (create_$name)");
- };
- #
- # Generate "/$name/:id" route, also handled by controller $name
- #
- # resource routes might be chained, so we need to define an
- # individual id and pass its name to the controller (idname)
- $resource = $r->route("/$plural/:${name}id")->to("$name#", idname => "${name}id");
- # GET requests - lists a single resource
- $resource->get->to('#rest_show')->name("show_$name");
- $app->log->info("Created route GET ".$r->to_string."/$plural/:${name}id (show_$name)");
- if (!$readonly) {
- # DELETE requests - deletes a resource
- $resource->delete->to('#rest_remove')->name("delete_$name");
- $app->log->info("Created route DELETE ".$r->to_string."/$plural/:${name}id (delete_$name)");
- # PUT requests - updates a resource
- $resource->put->to('#rest_update')->name("update_$name");
- $app->log->info("Created route PUT ".$r->to_string."/$plural/:${name}id (update_$name)");
- }
- return $resource;
- }
- );
- }
- 1;
- __END__
- =pod
- =head1 NAME
- Mojolicious::Plugin::CRUDRoutingHelper - routing helpers for HTTP CRUD
- =head1 VERSION
- version 0.2.1
- =head1 DESCRIPTION
- This Mojolicious plugin adds some routing helpers for CRUD operations via HTTP
- to the app. The routes are intended to be used by AJAX applications.
- =head1 EXTENDS
- =over 4
- =item * L<Mojolicious::Plugin>
- =back
- =head1 METHODS
- =head2 register
- Adds the routing helpers. Is called by Mojolicious.
- =head1 MOJOLICIOUS SHORTCUTS
- =head2 rest_routes
- Can be used to easily generate the needed RESTful routes for a resource.
- $self->rest_routes(name => 'user');
- # GET /api/users
- # POST /api/users
- # GET /api/users/:userid
- # DELETE /api/users/:userid
- # PUT /api/users/:userid
- $self->rest_routes(name => 'log', readonly => 1);
- # GET /api/logs
- # GET /api/logs/:logid
- The target controller has to implement the following methods:
- =over 4
- =item *
- create
- =item *
- update
- =item *
- list
- =item *
- show
- =item *
- remove
- =back
- B<Parameters>
- =over 4
- =item *
- name - name of the resource (will be used to generate the route and the controller name)
- =item *
- readonly - (optional) if set to 1, no create/update/delete routes will be created
- =back
- =head1 AUTHOR
- Jens Berthold E<lt>jens.berthold@jebecs.deE<gt>
- =head1 COPYRIGHT AND LICENSE
- Copyright 2002-2013 by Jens Berthold E<lt>jens.berthold@jebecs.deE<gt>.
- This library is free software; you can redistribute it and/or modify
- it under the same terms as Perl itself.
- =cut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement