Trailing slash is paths

I found there’s a route in my site that could only be reached if I type the trailing slash:

$app->group('/foo', function (RouteCollectorProxy $group) {
    $group->get('/', Pages\Foo::class); // 👈 This one

    $group->any('/logs.html', Pages\Foo::class . ':logs');
    $group->get('/scripts.html', Pages\Foo::class . ':scripts');
});

I fixed it by duplicating the route definition. It works, but it feels ugly.

    $group->get('/', Pages\Foo::class);
    $group->get('', Pages\Foo::class);

I also tried out out the middlewares/trailing-slash package linked in the Trailing / in route patterns chapter of Slim cook book to normalise trailing slashes in paths (I’ll probably force removal) but I can’t see that it makes any difference. I’ve tried adding it first and last, and nothing seems to happen. No redirection happens.

I found this snippet in the forum, but it throws ArgumentCountError in current version. It probably has an easy fix, but this silly issue is starting to get time consuming for me.

How do people deal with trailing slashes and canonical paths in Slim v4?

try this: $group→get(‘[/]’, Pages\Foo::class);

1 Like

Ah, yes. I totally missed optional segments. Thank you!

I eventually understood that the middleware package needs to be configured this way if you want a redirection:

$app->add(new TrailingSlash()->redirect()); // Don't miss ->redirect() !!!

Otherwise, it merely populates the internal URI field for the request object. I thought the examples in the documentation were illustrating something else.