5

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?

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
user2145475
  • 657
  • 3
  • 11

1 Answers1

4

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;
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • Thanks Evan - that's a good idea - although the controller may not know the name of the placeholder - as it is a generic controller for multiple routes. I'm trying to discover the name of the placeholder from within the controller - so it needs to somehow introspect to pick up the placeholder name. I read also that placeholder values are also placed into the stash. I could potentially search for a variable name that appears in the stash as well as in params to resolve the problem but this feels hacky. – user2145475 Sep 21 '21 at 08:11
  • maybe this answer can help? https://stackoverflow.com/questions/43141771/perl-mojolicious-under/62223541#62223541 (I mean source code of command that prints out all routes will help) – k-mx Sep 21 '21 at 09:55