Alois Mahdal
Alois Mahdal

Reputation: 11233

Slash in Dancer token

I would like to interpret, for example a request like this:

GET /my/path?foo=bar

just as if it was actually rewritten to e.g.

GET /?path=/my/path&foo=bar

Now I thought I'll be able to achieve this using following route, and use param('path') along with param('foo') and the likes, e.g.:

get '/:path' => sub {
    return printf "...so you want %s, thinking that best foo is %s...",
        param('path'),
        param('foo');
}

but I get 404 -- It seems that the :path part cannot contain slashes.

Can I achieve this with routes at all? Or I'm looking at the wrong direction (I'm fresh new to Dancer)?

Upvotes: 1

Views: 295

Answers (1)

Alois Mahdal
Alois Mahdal

Reputation: 11233

You might want to match route by regular expression instead of the token. Matches are then stored in a special array that can be returned by keyword splat. Your path will not be accessible by param('path'), though.

Code:

get qr{/([^?]*)} => sub {
    my ($path) = splat;
    return printf "...so you want %s, thinking that best foo is %s...",
        $path,
        param('foo');
}

Upvotes: 1

Related Questions