How to receive parameters
Now let's add functionality to the basic application. This time I would like to explain how to receive parameters in Mojolicious. Think of a parameter as the data sent by the user. There are three ways to receive the parameters.
- Part of the URL
- Query string
- POST data
This time, I would like to explain two of these, one is to receive it as a "part of the URL" and the other is to receive it as a "query string". I would like to explain how to send data as POST data again when explaining the use of forms. Keep in mind that there are three ways to do this for now.
Receive parameters as part of the URL
First, I'll show you how to receive the parameters as part of the URL. For example, consider the following URL:
/ date / 20131016 / date / 20140203
It contains the date as part of the URL. Let's see how to get the part of the date value when accessing with such a URL. Let's write the code.
use Mojolicious::Lite; # Specifying a routing pattern get'/ date /: date' => sub { my $self = shift; #Receiving parameters my $date = $self->param('date'); $self->render(text => "Data: $date"); };; app->start;
If you run this application and access it like "/ date / 20131016", you should see the date value.
Specifying a routing pattern
In order to receive the parameters, you need to define a routing pattern.
# Specifying a routing pattern get'/ date /: date' => sub {...};
Notice the ": date" part. If you want to make a part of the URL a parameter, write that part as ": name". If you write it like this, you can use the param method to receive the value as a parameter.
#Receiving parameters my $date = $self->param('date');
Notes on the param method
One thing to be careful about when using the param method. That's because the param method returns different results in the scalar context and the list context. In scalar context, the value of one parameter is returned. If the parameter has multiple values, the first value is returned. Multiple parameters are used, for example, when multiple values are selected, such as in a checkbox on a form.
On the other hand, when received in the list context, if there are multiple values for the parameter, multiple values will be acquired.
#Scalar context my $value = $self->param('foo'); #List context my @values = $self->param('foo');
For this reason, if you try to use the value by calling the param method directly, you may get unexpected results. To prevent mistakes in advance, it is safe to assign to a variable before using it.
Receive parameters as query string
Next, I will explain how to receive the parameter as a query string . The query string is the part that follows the URL and specifies the parameters. Please see the following URL.
/ diary /? date = 20131016 & user = kimoto / diary /? date = 20121013 & user = kimoto
The query string starts after the "?". Parameter names and values are separated by "=". You can concatenate multiple parameters by using "&". In the first URL, there are two parameter names, "date" and "user". The corresponding values are "20131016" and "kimoto".
Now let's get the value of the parameter.
use Mojolicious::Lite; # Specifying a routing pattern get'/ diary' => sub { my $self = shift; #Receiving parameters my $date = $self->param('date'); my $user = $self->param('user'); $self->render(text => "Date: $date, User: $user"); };; app->start;
When you start this application and access it with the URL "/ diary /? Date = 20131016 & user = kimoto", "Date: 20131016, User: kimoto" is displayed, and you can see that the parameters have been acquired.
Get parameters
Use param to get the parameters. The param method is generically designed and parameters can be received as part of a URL or from a query string.
#Receiving parameters my $date = $self->param('date'); my $user = $self->param('user');
These parameters allow the data sent by the user to be used in the program.