Creating a Base controller

So you want to create a base controller for your application to make your code a little more reusable?

Here's how I did it.

I built a small API that can have multiple protocol backends (XMLRPC/REST/SOAP/etc). The base class was called API, and the sub classes are API::REST, API::SOAP, API::XMLRPC, etc.

The code

API:

package OTBAN::Controller::API;
#use Moose::Role; # doesn't work  quite yet with actions/components
use base 'Catalyst::Controller';


=head1 NAME

OTBAN::Controller::API - Catalyst Controller

=head1 DESCRIPTION

Base class for API methods.

=head1 METHODS

=cut


=head2 index


=cut

sub index : Private {

    my ($self, $c) = @_;
    return "Index!";

}


=head2 post

post a new thread

=cut

sub post : Private {
    my ($self, $c) = @_;

    return "Post an item";

}

=head2 put

put/update an item

=cut

sub put : Private {
    my ($self, $c) = @_;

    return "update an item";

}

=head2 get

get a single item

=cut

sub get : Private {
    my ($self, $c) = @_;

    return "get an item";

}

=head2 delete

delete an item

=cut

sub delete : Private {
    my ($self, $c) = @_;

    return "delete an item";

}

=head2 list

list all items

=cut

sub list : Private {
    my ($self, $c) = @_;

    return "list all items";

}

=head2 item

list one item

=cut

sub item : Private {
    my ($self, $c) = @_;

    return "list one item";

}


=head1 AUTHOR

Devin Austin
http://www.codedright.net
dhoss@cpan.org

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;

API::REST:

package OTBAN::Controller::API::REST;
use Moose;
BEGIN { extends 'Catalyst::Controller::REST' };
use base 'OTBAN::Controller::API';


## thanks lukes! (luke.saunders)
__PACKAGE__->config(
    'default'   => 'application/json',
    'stash_key' => 'response',
        'map'       => {
            'application/x-www-form-urlencoded' => 'JSON',
                'application/json'                  => 'JSON',
});


=head1 METHODS

=cut


=head2 index

Set up our ActionClass as REST

=cut

sub index : ActionClass('REST') { 
    my ($self, $c) = @_;
}


=head2 thread_setup

set up rest stuff for a thread

=cut

sub thread_setup : Chained('/') PathPart('threads') CaptureArgs(1) {
    my ($self, $c, $thread) = @_;

    ## do checking to see if $thread exists
    $c->stash->{thread} = $thread;


}

=head2 thread

set up our REST actionclass

=cut

sub thread : Chained('thread_setup') PathPart('request') Args(0) ActionClass('REST') {

}

=head2 thread_POST

create a thread. defaults to a parent thread.

=cut


sub thread_POST {
    my ($self, $c) = @_;

    $c->forward('post');

}


=head2 thread_GET

get a thread's information.
this gets a given thread in its entirety, with children.

=cut

sub thread_GET {
    my ($self, $c) = @_;

    $c->forward('item');


}

=head2 thread_PUT
=cut

sub thread_PUT{
    my ($self, $c) = @_;

    $c->forward('update');

}

=head2 thread_DELETE

delete a thread and it's children.

=cut

sub thread_DELETE {
    my ($self, $c) = @_;

    $c->forward('delete');
}

1;

http://localhost:3000/thread/id/request will send a GET request that is handled by thread_GET, which forwards to the private action list.

My tags:
 
Popular tags:
  rest moose base controller
Powered by Catalyst
Powered by MojoMojo Hosted by Shadowcat - Managed by Nordaaker