Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

When I parse html response body I want to find route names for all links found in the body. I use next code snippet:

    my $url =  Mojo::URL->new( $got );
    my $method =  uc( $url->query->clone->param( '_method' ) || 'GET' );
    my $c =  $t->app->build_controller;
    my $m =  Mojolicious::Routes::Match->new( root => $t->app->routes );

    $m->find( $c => { method => $method,  path => $url->path } );

Then $m->endpoint->name gives me the name of route.

But is there more simple way to find route name by given path?

I am looking for something like: $app->routes->find( '/api/v/users/146/link/7QRgs' ) which should return user_hash_check because I have next route:

$guest->get( '/users/:id/link/:hash', 'user_hash_check' )->to( 'user#hash_check' );
share|improve this question
    
Out of curiosity, when do you use this? Are your parsing your own application's website? – simbabque Jan 4 at 15:12
    
@simbabque: Yes, I do. Actually I use this in tests – Eugen Konkov Jan 4 at 15:15
    
Interesting approach. Is simply clicking the links not sufficient? – simbabque Jan 4 at 15:16
    
What's the problem with the solution you posted? Is it expensive? Or did you just forget you can create a sub? – ikegami Jan 4 at 15:19
1  
Doesn't sound very common to me! – ikegami Jan 4 at 15:23

I have found only one place where we can find route by path. That is Mojolicious::Routes::Match and there is no other way to do this

The one ugly thing here to my mind is requirement to supply Mojolicious::Controller object. But controller is only required to make decision: dispatch or not dispatch Because it has extra info to make this decision: this is data to check conditions

The problem as I think is because of here are mixed two things:

  1. Request
  2. Path

And find should just return all routes matched against given arguments: path and method. Like selectors does The array result maybe cached (now routes with conditions are not cached)

Then dispatcher should check conditions against each route, not matcher. Here each condition may be called in context of right controller and not default one. And this will fix this issue. The routes have in most cases their own controller class have not?

Until this behavior will be fixed the example in the question is the best way to find routes

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.