Home > Software design >  Introspecting the name of placeholder variables from a Mojolicious controller
Introspecting the name of placeholder variables from a Mojolicious controller

Time:09-22

I have a Mojolicious controller that fires in response to different URL routes. For example, given the URL path:

/v1/users/:someid

and a controller that fires:

sub handle_request ($self) {
     my $place_holder_name = $self->route->??????   # how can I get 'someid'?
     is($place_holder_name, 'someid', 'can access the placeholder name');
}

How can I find out the name of the placeholder?

CodePudding user response:

Param

These are not currently documented under Mojolicious::Routes, so I can see why that's confusing. They're documented under Mojolicious::Controller#param,

What you have there is a Route param, so you can retreive that value with,

$c->param('someid');

Getting All Params Provided to a Controller

Though undocumneted, you can find the names of the captures in the internal hashref like this,

$self->stash->{'mojo.captures'};

Like this;

my $params = $self->stash->{'mojo.captures'};
warn for keys %$params;
  • Related