Mojolicious::Controller::REST - Mojolicious Controller for RESTful operations
version 0.003
# In Mojolicious Controller use Mojo::Base 'Mojolicious::Controller::REST'; $self->data( hello => 'world' )->message('Something went wrong'); # renders json response as: { "data": { "hello": "world" }, "messages": [ { "severity": "info", "text": "Something went wrong" } ] }
Mojolicious::Controller::REST helps with JSON rendering in RESTful applications. It follows and ensures the output of the method in controller adheres to the following output format as JSON:
{ "data": { "<key1>": "<value1>", "<key2>": "<value2>", ... }, "messages": [ { "severity": "<warn|info>", "text": "<message1>" }, { "severity": "<warn|info>", "text": "<message2>" }, ] }
Mojolicious::Controller::REST extends Mojolicious::Controller and adds below methods
Sets the data element in 'data' array in JSON output. Returns controller object so that other method calls can be chained.
Sets an individual message in 'messages' array in JSON output. Returns controller object so that other method calls can be chained.
A custom severity value can be used by calling message as:
$self->message('Something went wrong', 'fatal'); # renders json response as: { "messages": [ { "text": "Something went wrong", "severity": "fatal" } ] }
Similar to message, but with severity = 'warn'. Returns controller object so that other method calls can be chained.
Set the status of response. Returns controller object so that other methods can be chained.
Abhishek Shende <abhishekisnot@gmail.com>
This software is copyright (c) 2014 by Abhishek Shende.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Mojolicious::Plugin::REST, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::REST
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::REST
For more information on module installation, please visit the detailed CPAN module installation guide.