Application and controller functions

Mojolicious has a lot of useful features for creating web applications. Among them, I would like to introduce the ones that are frequently used. If you remember these first, you won't have to worry about what to do.

Application object

You can get the object that represents the body of the application with the app function. The app function is automatically imported when you load Mojolicious::Lite.

use Mojolicious::Lite;

#Application object
my $app = app;

The application object belongs to the Mojolicious class and you can call the methods of the Mojolicious class. See Mojolicious API Reference for a list of methods.

Home object

First, you can get a home object that represents Mojolicious's home directory using the home method. Home objects belong to the Mojo::Home class.

#Get home object
my $home = $app->home;

From the home object, you can call the methods of the Mojo::Home class. Mojo::Home has a method called rel_file , which converts the path specified by the relative path from the home directory to the absolute path.

my $path_abs = $home->rel_file('db / myapp.db');

This is useful when getting the path to a database file, etc.

Log file

Mojolicious has a mechanism to output logs. Try creating a directory called log in the same directory as your application.

myapp.pl
log log

Then, a file called development.log will be created in this directory, and the application log will be output here.

log / development.log

You can output it to this log file yourself, but to do so, use the log method from the application object to get the log object.

my $log = $app->log;

Log objects belong to the Mojo::Log class. The Mojo::Log class can output logs according to the level. You can output the log according to the level using the following method.

$log->debug($messgae);
$log->info($messgae);
$log->warn($messgae);
$log->error($messgae);
$log->fatal($messgae);

Information that you want to output only when debugging is output with debug . Normally, the info method outputs information, and in the case of warnings, errors, and fatal errors, the warn method, error method, You may want to use the fatal method.

It is up to each programmer to decide what is an error and what is a warning.

Configuration file

Mojolicious makes it easy to write configuration files. Create a file with the same name as the script and the extension conf in the same directory as the script.

myapp.pl
myapp.conf

This configuration file can be written in Perl.

{
  name =>'kimoto',
  age => 19
}

Note that you write a hash reference. If you use Japanese, save the file in UTF-8.

Read the configuration file

The configuration file can be read by using the Config plugin. A plugin is a mechanism to add Mojolicious functions, and a Config plugin is a plugin that is easy from the beginning to read a configuration file.

To use a plugin, call the plugin method from a plugin function or application object.

plugin ('Config');
$app->plugin('Config');

Now the config file is loaded and you can get the config with the config method.

# Get one by one
my $name = $app->config('name');
my $age = $app->config('age');

# Get all
my $config = $app->config;

It is also possible to make settings on the application.

# Setting
$app->config('name' =>'Ken');

Get application object with controller

To get the application object from the controller, use the app method from the controller object.

my $app = $c->app;

For example, you can get: If you can get the application object, you can also call home or log from here.

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

  my $app = $self->app;
  $app->log->info('Info');
};;

Placement of static files

If you want to use static files such as CSS, JavaScript and images, create a public directory in the same directory as your application.

myapp.pl
public

And place a static file such as CSS in this.

public / css / common.css
public / js / common.js

Static files are automatically dispatched by Mojolicious. You can get these files by accessing them at the following URL:

http: //localhost:3000/css/common.css
http: //localhost:3000/js/common.js

Template external file

Mojolicious::Lite used to write templates in one file, but as your application grows, you'll want to write it in an external file.

Take a look at the following application.

use Mojolicious::Lite;

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

  $self->render('info');
};;

app->start;

__DATA__

@@info.html.ep
Information

info.html.ep is written in the data section, but let's convert this template to an external file.

First, create a directory called templates in the same directory as the script.

myapp.pl
templates templates

Place the template file in this.

templates / info.html.ep

And the data section will be deleted.

use Mojolicious::Lite;

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

  $self->render('info');
};;

app->start;

Externalizing the template file in this way is convenient because it makes it easier to understand the line number where the syntax error occurred.

Redirection

A redirect is the ability to forward to another URL. You can redirect using the redirect_to method of the Mojolicious::Controller class.

$c->redirect_to('/ other');

Samples using these functions

Finally, let's create a sample using the functions introduced here.

myapp.pl

First, write the body of the application.

use Mojolicious::Lite;

#Read configuration file
plugin ('Config');

#Output to log
app->log->info('Start application');

#Get the path of the database file
my $db_file = app->home->rel_file('db / myapp.db');

Access to # /
get'/' => sub {
  my $self = shift;

  # application
  my $app = $self->app;

  # Output to log
  $app->log->info('Access infomation');
  
  #Get the path of the database file
  my $db_file = $app->home->rel_file('db / myapp.db');

  #Get from config file
  my $name = $app->config('name');
  my $age = $app->config('age');

  #Drawing a template
  $self->render(
    'info',
    name => $name,
    age => $age,
    db_file => $db_file
  );
};;

#Redirect
get'/ some' => sub {
  my $self = shift;

  $self->redirect_to('/ other');
};;

get'/ other' => sub {
  my $self = shift;

  $self->render(text =>'Other');
};;

app->start;

log

Create a log directory for the log files.

myapp.conf

This is a configuration file. Place it in the same directory as myapp.pl.

{
  name =>'Kimoto',
  age => 32
}

public / css / common.css

It is a style sheet. Create a public directory and place it in it.

body {
  background: #FFFFEE;
}

templates / info.html.ep

This is a template file. Create a templates directory and place it in it.

<%
  my $name = stash ('name');
  my $age = stash ('age');
  my $db_file = stash ('db_file');
%>

<html>
  <head>
    <title> Information </title>
    %= stylesheet'/ css / common';
  </head>
  <body>
    <b> Name </b>: <%= $name%> <br>
    <b> Age </b>: <%= $age%> <br>
    <b> Database file </b>: <%= $db_file%> <br>
  </body>
</html>

The stylesheet function isn't covered here, but it's a template helper for loading stylesheets.

Run application

Check if the directory structure is as follows.

myapp.pl
myapp.conf
templates / info.html.ep
public / css / common.css
log log

If you run the application and access the "/", you will get the following results:

Name: Kimoto
Age: 32
Database file: /home/kimoto/labo/db/myapp.db

If you access "/ some", you will be redirected to "/ other" and the contents of "/ other" will be displayed.

Other Other

Associated Information