简体   繁体   English

Slim 3路由参数

[英]Slim 3 route parameter

I encountered strange square bracket in Slim-Skeleton routes.php 我在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. 我试着查看文档,但它什么也没给我。

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 / . 所以路由$app->get('/[{name}]'匹配任何URL字符串,包括none /

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. 此外,[...]中包含的路径部分被认为是可选的,因此/ foo [bar]将匹配/ foo和/ foobar。 Optional parts are only supported in a trailing position, not in the middle of a route. 可选部件仅支持在尾随位置,而不是在路径中间。 - nikic/FastRoute - nikic / FastRoute

Slim is built on top of FastRoute. Slim建立在FastRoute之上。 See defining routes to learn more about the FastRoute routing syntax. 请参阅定义路由以了解有关FastRoute路由语法的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM