Perl Dancer Trailing Slash

Using the Perl web application platform, Dancer, I am having problems with trailing slashes in the URL mapping.

Say, for example, I want to map the following URL to an optional Id parameter:

get '/users/:id?' => sub
{
    #Do something
}

Both /users/morganand /users/match. Although /usersit will not. Which does not seem very uniform. As I would prefer, only matching URL: s without a trailing slash: /users/morganand /users. How can i achieve this?

+5
source share
2 answers

Another approach is to use a named subsection - all Dancer code examples typically use anonymous subtitles, but there is nothing to suggest that it should be anonymous.

get '/users' => \&show_users;
get '/users/:id' => \&show_users;

sub show_users
{
    #Do something
}

, - , Dancer , , , .

+7

id /user/ .

get qr{^/users/?(?<id>[^/]+)?$} => sub {
  my $captures = captures;
  if ( defined $captures->{id} ) {
    return sprintf 'the id is: %s', $captures->{id};
  }
  else {
    return 'global user page'
  }
};
+5

All Articles