问题描述
我正在使用 Slim PHP 作为 RESTful API 的框架,到目前为止它很棒.超级容易使用,但我确实有一个问题找不到答案.如何从 Slim PHP 中的 URL 获取 GET 参数?
I'm playing with Slim PHP as a framework for a RESTful API, and so far it's great. Super easy to work with, but I do have one question I can't find the answer to. How do I grab GET params from the URL in Slim PHP?
例如,如果我想使用以下内容:
For example, if I wanted to use the following:
http://api.example.com/dataset/schools?zip=99999&radius=5
星期一的情况?是我想多了?提前致谢!
A case of the Mondays? Am I overthinking it? Thanks in advance!
推荐答案
您可以在 Slim 框架内非常轻松地做到这一点,您可以使用:
You can do this very easily within the Slim framework, you can use:
$paramValue = $app->request()->params('paramName');
$app 这里是一个 Slim 实例.
$app here is a Slim instance.
或者如果你想更具体
//获取参数
$paramValue = $app->request()->get('paramName');
//POST 参数
$paramValue = $app->request()->post('paramName');
您会在特定路线中像这样使用它
You would use it like so in a specific route
$app->get('/route', function () use ($app) {
$paramValue = $app->request()->params('paramName');
});
您可以阅读有关请求对象的文档http://docs.slimframework.com/request/variables/
You can read the documentation on the request objecthttp://docs.slimframework.com/request/variables/
截至 Slim v3:
$app->get('/route', function ($request, $response, $args) {
$paramValue = $request->params(''); // equal to $_REQUEST
$paramValue = $request->post(''); // equal to $_POST
$paramValue = $request->get(''); // equal to $_GET
// ...
return $response;
});
这篇关于精简 PHP 和 GET 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!