Describe various routes with Mojolicious::Lite

Let's write various routes with Mojolicious::Lite.

GET and POST methods

The route to "/" by the GET method. The GET method is often used when retrieving a page or performing a search.

# Via get method
use Mojolicious::Lite;
get'/' => sub {shift->render(text =>'Hello')};
app->start;

Next is the root to "/ create" by the POST method. The POST method is often used to make changes to data that resides on the server, such as posting, updating, or deleting new articles. Generally, you can think of it as being used in the form submission process.

# Via post method
use Mojolicious::Lite;
get'/' =>'index';
post'/ create'=> sub {
  my $self = shift;
  
  my $title = $self->param('title');
  
  $self->render(text => $title);
};;
app->start;

__DATA__

@@index.html.ep
<html>
  <head> <title> Top Page </title> </head>
  <body>
    <form method = "post" action = "<%= url_for'/ create'%>">
      <div> Title: <input type = "text" name = "title"> </div>
      <div> <input type = "submit" value = "Send"> </div>
    </form>
  </body>
</html>

Use placeholders

You can include placeholders in the URL. Start with a colon, like ": name". You can think of placeholders as variables.

#Place holder
get'/ entry /: date' => sub {
  my $self = shift;
  
  my $date = $self->param('date');
  
  $self->render(text => $date);
};;

The value specified in the placeholder can be obtained by the param method.

Let's access it with the following URL. It is displayed as 20111212.

/ entry / 20111212

Relax Placeholder

However, this placeholder cannot contain. (Dot). If you also want to capture. Use the Relax Placeholder. Relax placeholders start with a sharp like "#name". Note that relax placeholders should always be enclosed in parentheses "()". (Note: From around Mojolicious 3.0, the notation for relax placeholders has changed from ".name" to "#name".)

#Relax place holder
get'/ entry / (.datetime)' => sub {
  my $self = shift;
  
  my $datetime = $self->param('datetime');
  
  $self->render(text => $datetime);
};;

Let's access it with the following URL. It is displayed as 20111212.102232.

/entry/20111212.102232

Wildcard Placeholder

Use wildcard placeholders if you want to match including / (slash).

#Wildcard place holder
get'/ entry / * datetime' => sub {
  my $self = shift;
  
  my $datetime = $self->param('datetime');
  
  $self->render(text => $datetime);
};;

Let's access it with the following URL. It is displayed as 20111212/102232.

/ entry / 20111212/102232

Root technique

Two routes

What if you want to execute the same logic on the following two URLs?

/ entry
/ entry / 20101111

In such a case, it is better to set the default value to undef. Then you can use the same logic in the above two routes.

#Match above two URL
get'/ entry /: date'=> {date => undef} => sub {
  my $self = shift;
  
  my $date = $self->param('date') ||'undefined';
  
  $self->render(text => $date);
};;

Mixing static and dynamic routes

What if you want to mix static and dynamic routes?

#Static route
/ description

#User name
/ kimoto
/ kraih
/ taro

In that case, all you have to do is write a static route at the top.

#Static route
get'/ description' => sub {shift->render(text =>'description')};

#Dinamic route
get'/: user' => sub {
  my $self = shift;
  
  my $user = $self->param('user');
  
  $self->render(text => $user);
};;

Let's write a slightly more difficult route. URLs such as Github also have this kind of mechanism.

#Description (Static)
/ description

#User name (Dinamic)
/ kimoto
/ kraih
/ taro

#User name and profile (Dinamic and Static)
/ kimoto / profile
/ kraih / profile
/ taro / profile

#User name and project (Dinamic and Dinamic)
/ kimoto / dbix-custom
/ kraih / mojo
/ taro / foobar
#Description (Static)
get'/ description' => sub {shift->render(text =>'description')};

#User name (Dinamic)
get'/: user' => sub {
  my $self = shift;
  
  my $user = $self->param('user');
  
  $self->render(text => "User $user");
};;

#User name and profile (Dinamic and Static)
get'/: user / profile'=> sub {
  my $self = shift;
  
  my $user = $self->param('user');
  
  $self->render(text => "$user Profile");
};;

#User name and project (Dinamic and Dinamic)
get'/: user /: project' => sub {
  my $self = shift;
  
  my $user = $self->param('user');
  my $project = $self->param('project');
  
  $self->render(text => "$user $project Project");
};;

Associated Information