【问题标题】:Slim 3 route parameterSlim 3 路由参数
【发布时间】:2017-10-16 00:13:17
【问题描述】:

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

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

【问题讨论】:

    标签: php slim slim-3


    【解决方案1】:

    From the docs:

    可选部分

    要使部分可选,只需用方括号括起来

    所以路由 $app->get('/[{name}]' 匹配任何 URL 字符串,包括不匹配 /

    【讨论】:

      【解决方案2】:

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

      包含在 [...] 中的路由的其他部分被认为是可选的,因此 /foo[bar] 将匹配 /foo 和 /foobar。可选部件仅支持在尾随位置,而不是在路线中间。 - nikic/FastRoute

      Slim 建立在 FastRoute 之上。请参阅defining routes 了解有关 FastRoute 路由语法的更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-30
        • 1970-01-01
        • 2017-01-05
        • 2013-01-09
        • 1970-01-01
        • 2016-01-01
        • 2015-02-14
        • 2018-12-04
        相关资源
        最近更新 更多