Dancer: Receive parameter in route
- param
- subtest
Each URL path can be mapped to a specific function, but we can also map a whole set of URLs to a single function and use parts of the URL path as parameters. For example we might want to show information about each user via their profile URL which is /user/ID where the ID is their user id. (For public URL it is probably a better idea to let them have a unique username and use that, but the basic concept is the same.)
We can set it up in the following way:
package App;
use Dancer2;
get '/' => sub {
return q{
<a href="/user/1">One</a><br>
<a href="/user/2">Two</a><br>
<a href="/user/foobar">foobar</a><br>
<a href="/user">user</a><br>
<a href="/user/">user/</a><br>
<a href="/user/a/b">a/b</a><br>
<a href="/user/-1">-1</a><br>
<a href="/user/1.1">1.1</a><br>
};
};
get '/user/:id' => sub {
my $id = route_parameters->get('id');
return $id;
};
App->to_app;