1

I have a Lumen project (Lumen 5.2) and I need to do two different things for two identical urls except for an ending slash '/'. For example:

http://example.com/some/path   --> Show a resource named 'path'
http://example.com/some/path/  --> List the content of the 'path' folder

But I didn't found a way to write routes able to capture such difference.

I tried with this:

$app->get('/{p:.*}', function ($p) use ($app) {
  return 'Case 1: ' . $p;
});

$app->get('/{p:.*}/', function ($p) use ($app) {
  return 'Case 2: ' . $p;
});

But both urls (/some/path and /some/path/) are captured by the last route and $p is some/path in both cases, so I can't know what url it was.

Is there a way to solve this?

Andrea
  • 15,900
  • 18
  • 65
  • 84
  • Have you tried to capture it in the same route then inside the closure detecting if there is a trailing slash? – ddelnano Jan 12 '16 at 19:01
  • Just a hunch...try defining the route with the trailing slash first. Probably won't do it but worth a shot. – Ben Harold Jan 12 '16 at 19:40
  • @ddelnano yes, I tried with only this route: `'/{p:.*}'`, but with or without the trailing slash in the url, the parameter `$p` is always `some/path` (without the slash)... – Andrea Jan 12 '16 at 19:42
  • @BenHarold hehe :).. yes, I tried to put the second route as first! But sadly the result is almost the same: for any url the last defined route will catch it and the parameter `$p` doesn't have the trailing slash (`$p` is always `some/path`)... :( – Andrea Jan 12 '16 at 19:48
  • The .htaccess file Laravel ships with will redirect if there is a trailing slash to the non-trailing slash version. – user1669496 Jan 12 '16 at 19:48
  • @user3158900 So, is there no way to get the trailing slash version inside the router? – Andrea Jan 12 '16 at 20:12

0 Answers0