Mojolicious's useful API

Request object

The request object can be obtained through the controller object.

get'/' => sub {
  my $self = shift;
  my $req = $self->req;
};;

The request object is Mojo::Message::Request. Mojo::Message::Request is Mojo::Message.

Introducing a convenient API for request objects.

params

You can use the params method to access a parameter object that holds the parameters passed by GET and POST.

my $params = $req->params;

query_params

You can use the query_params method to access a parameter object that holds the parameters passed in the GET.

my $params = $req->query_params;

json

You can use the json method to get the data sent in JSON format as a Perl data structure.

my $data = $req->json;

is_secure

my $secure = $req->is_secure;

Check if the connection is secure. In other words, check if you are connected with "https: //".

is_xhr

$xhr = $req->is_xhr;

Checks if the "Check X-Requested-With" header contains the string "XMLHttpRequest". In other words, it checks whether it is communicating with AJAX.

Parameter object

A parameter object is an object that holds the parameters passed by GET or POST. This is a Mojo::Parameters object.

Introducing a useful API for parameter objects.

to_hash

You can use the to_hash method to convert a parameter to a hash reference.

my $hash = $params->to_hash;

I think this is often used from controller objects like this:

get'/' => sub {
  my $self = shift;
  my $data = $self->req->params->to_hash;
}

Application object

Application objects can be obtained using the app function.

my $app = app;

The application object is a Mojolicious object. Mojolicious is Mojo.

Introducing a useful API for application objects.

home

Use the home method to get the home object.

my $home = $app->home;

The home object is a Mojo::Home object that holds information about your application's home directory. increase.

log

Use the log method to get the log object.

my $log = $app->log;

The log object is a Mojo::Log object.

secrets

Use the secrets method to set a password for encrypting cookies.

$app->secrets(['lkjiji! & F']);

Be sure to set it when managing sessions using cookies.

start

Use the start method to start the application.

$app->start;

types

Use the types method to get the types object.

my $types = $app->types;

The type object is a MojoX::Types object. This is an object that defines a file extension and content type mapping.

defaults

Use the defaults method to set the default values ​​for the stash.

#Set
$app->defaults({foo =>'bar'});
$app->defaults->{foo} ='bar';

#Get
my $foo = $app->defaults->{foo};

helper

Use the helper method to add a helper method.

$app->helper(
  foo => sub {...},
  bar => sub {...}
);

The helper method added by the helper method can be used inside the application object, controller object, and template.

$app->foo;
$c->foo;
@@index.html.ep
%foo;

mode

Use the mode method to get and set the mode of the application.

my $mode = $app->mode;
$app->mode('production');

This can also be done using environment variables.

export MOJO_MODE = production

Functions imported by Mojolicious::Lite

A function that is imported when you load Mojolicious::Lite. (For more information on Mojolicious::Lite, please refer to Mojolicious::Lite documentation. )

app

To get the application object, define the app function.

my $app = app;

get

Use the get function to define the processing for the URL accessed by the HTTP GET method. ..

get'/: foo' => sub {...};

post

Use the post function to define the processing for the URL accessed by the HTTP POST method.

post'/: foo' => sub {...};

any

Use the any function to define the processing for URLs accessed by any HTTP method. .. You can also specify multiple HTTP methods.

any'/: foo'=> sub {...};
any [qw / get post head put delete /] =>'/: foo' => sub {...};

under

Use the under function to define the process you want to execute before executing the process defined by the get or post method. This can be used for authentication etc.

under sub {...};

websocket

Use the websocket function to define the processing for URLs accessed by the WebSocket handshake (connecting to a WebSocket).

websocket'/: foo' => sub {...};

plugin

Use the plugin function to add a plugin.

plugin'JsonConfig';

The default namespace is Mojolicious::Plugin, and you can load the plugin by specifying the module name under this.

Associated Information