Slim 3路由参数

Slim 3 route parameter

我在 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);
});

为什么要用方括号?我试图查看文档,但它什么也没给我。

From the docs:

Optional segments

To make a section optional, simply wrap in square brackets

因此路由 $app->get('/[{name}]' 匹配任何 URL 字符串,包括 none /.

方括号表示路由参数是可选的。

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 建立在 FastRoute 之上。请参阅 defining routes 了解有关 FastRoute 路由语法的更多信息。