NAME
Ado::Control - The base class for all controllers!
SYNOPSIS
It must be inherited by all controllers. Put code here only to be shared by it's subclasses or used in hooks.
package Ado::Control::Hello;
use Mojo::Base 'Ado::Control';
ATTRIBUTES
Ado::Control inherits all attributes from Mojolicious::Controller and implements the following new ones.
description
Returns a default description used in head
element of HTML pages.
keywords
Returns default keywords used in head
element of HTML pages.
generator
Returns the concatenated moniker, VERSION and CODENAME.
SUBROUTINES/METHODS
Methods shared among subclasses and in hooks
config
Overwrites the default helper "config" in Mojolicious::Plugin::DefaultHelpers which is actually an alias for "config" in Mojo. Returns configuration specific to the current controller package only.
#in Ado::Control::List or Ado::Control::Foo or...
my $myvalue = $c->config('mykey');
#a shortcut to
my $myvalue = $app->config(__PACKAGE__)->{mykey}
...
To access the application-wide configuration use $c->app->config('key')
.
debug
A shortcut to:
$c->app->log->debug(@_);
list_for_json
Prepares a structure suitable for rendering as JSON for listing Ado::Model* objects returned by "select_range" in Ado::Model and returns it. Accepts two ARRAYREF
s as parameters:
my $res = $c->list_for_json([$limit, $offset], \@list_of_objects);
Use this method to ensure uniform and predictable representation across all listing resources. See http://127.0.0.1:3000/ado-users/list.json for example output and "list" in Ado::Control::Ado::Users for the example source.
my @range = ($c->param('limit') || 10, $c->param('offset') || 0);
return $c->respond_to(
json => $c->list_for_json(
\@range,
[Ado::Model::Users->select_range(@range)])
);
require_formats
Require a list of relevant formats or renders "415 - Unsupported Media Type" with a text/html type and link to the resource using the first of the preferred formats, and returns false. If the URL is in the required format, returns true. Adds a header Content-Location
with the proper URL to the resource.
#in an action serving only json
sub list {
my $c = shift;
$c->require_formats(['json']) || return;
$c->debug('rendering json only');
#your stuff here...
return;
}
This method exists only to show more descriptive message to the end user and to give a chance to user agents to go to the proper resource URL.
validate_input
Uses "validation" in Mojolicious::Controller to validate all input parameters at once given a validation template. The template consists of keys matching the input parameters to be validated. The values are HASH references describing the rules. Each rule name corresponds to a method/check in "CHECKS" in Mojolicious::Validator. You can use your own checks if you add them using "add_check" in Mojolicious::Validator.
Returns a HASH reference. In case of errors it contains errors
and json
HASH references. In case of success contains only output
HASH reference from "output" in Mojolicious::Validator::Validation.
my $rules = {
to_uid => {
'required' => 1, like => qr/^\d{1,20}$/
},
subject => {
'required' => 1, like => qr/^.{1,255}$/
},
#...
}
my $result = $c->validate_input($rules);
#400 Bad Request
return $c->render(
status => 400,
json => $result->{json}
) if $result->{errors};
SEE ALSO
Mojolicious::Controller, Ado::Manual::Controllers, Ado::Manual::RESTAPI
AUTHOR
Красимир Беров (Krasimir Berov)
COPYRIGHT AND LICENSE
Copyright 2013 Красимир Беров (Krasimir Berov).
This program is free software, you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License v3 (LGPL-3.0). You may copy, distribute and modify the software provided that modifications are open source. However, software that includes the license may release under a different license.
See http://opensource.org/licenses/lgpl-3.0.html for more information.