使用 Slim 3 和内置 PHP 服务器的路由参数

Route parameters with Slim 3 & built-in PHP server

我在获取带有参数的路由以在 Slim 3 RC 中工作时遇到问题。

$app->get('/hello/:name', function($req, $res, $args) {
    echo "Hello {$name}";
});

访问 /hello/joe 结果为 404。

其他路线也可以,例如:

$app->get('/', HomeAction::class . ":dispatch");

$app->get('/services', ServicesAction::class . ":dispatch");

我在开发时使用内置的 PHP 服务器。我没有任何 .htaccess 文件。我已经尝试了建议的 route.php 建议和 接受的答案,但它不起作用。有什么建议吗?

从 Slim 3 开始,您需要在 {name} 中更改 :name

$app->get('/hello/{name}', function ($request, $response, $args) {
    return $response->write("Hello " . $args['name']);
});

您可以找到文档 here