slim框架如何获得query参数?-灵析社区

0offer糕手

在slim框架中: get('/hello/{name}', function (Request $request, Response $response, array $args) { $name = $args['name']; $response->getBody()->write("Hello, $name"); return $response; }); 我们可以访问: [http://localhost:8080/hello/bob](https://link.segmentfault.com/?enc=plKq5jvYOa3UoasA%2B%2B9Ugw%3D%3D.M4TGl2YxDOizLOCLK3Q1eN087E9xTRMMg693515xwio%3D) ![image.png](https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241030/b6b6754d80086bc32489a3875219b7ad.png) 请问如何在slim获得query参数呢,比如访问: http://localhost:8080/hello/bob?aaa=111&bbb=222 搜索无结果。

阅读量:11

点赞量:0

问AI
可通过如下方式获取: get('/hello/{name}', function (Request $request, Response $response, array $args) { // 获取路径参数 $name = $args['name']; // 获取查询参数 $queryParams = $request->getQueryParams(); // 获取特定的查询参数 $aaa = isset($queryParams['aaa']) ? $queryParams['aaa'] : null; $bbb = isset($queryParams['bbb']) ? $queryParams['bbb'] : null; // 构建响应字符串 $greeting = "Hello, $name"; if ($aaa !== null && $bbb !== null) { $greeting .= " - aaa: $aaa, bbb: $bbb"; } // 将响应写入响应体 $response->getBody()->write($greeting); return $response; }); $app->run();