Terry Djony
Terry Djony

Reputation: 2015

Slim 3 route parameter

I encountered strange square bracket in Slim-Skeleton routes.php

$app->get('/[{name}]', function (Request $request, Response $response, array $args) {
    // Sample log message
    $this->logger->info("Slim-Skeleton '/' route");

    // Render index view
    return $this->renderer->render($response, 'index.phtml', $args);
});

Why use square bracket? I tried to look at the documentation but it gives me nothing.

Upvotes: 2

Views: 2142

Answers (2)

Scriptonomy
Scriptonomy

Reputation: 4055

Square brackets mean that route parameter is optional.

Furthermore parts of the route enclosed in [...] are considered optional, so that /foo[bar] will match both /foo and /foobar. Optional parts are only supported in a trailing position, not in the middle of a route. - nikic/FastRoute

Slim is built on top of FastRoute. See defining routes to learn more about the FastRoute routing syntax.

Upvotes: 1

Don't Panic
Don't Panic

Reputation: 14520

From the docs:

Optional segments

To make a section optional, simply wrap in square brackets

So the route $app->get('/[{name}]' matches any URL string, including none /.

Upvotes: 3

Related Questions