精简 PHP 和 GET 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8125064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:00:16  来源:igfitidea点击:

Slim PHP and GET Parameters

phprestslim

提问by Eric Arenson

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?

我正在使用 Slim PHP 作为 RESTful API 的框架,到目前为止它很棒。超级容易使用,但我确实有一个问题找不到答案。如何从 Slim PHP 中的 URL 获取 GET 参数?

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!

星期一的情况?是我想多了?提前致谢!

回答by Martijn

You can do this very easily within the Slim framework, you can use:

你可以在 Slim 框架中很容易地做到这一点,你可以使用:

$paramValue = $app->request()->params('paramName');

$app here is a Slim instance.

$app 这里是一个 Slim 实例。

Or if you want to be more specific

或者如果你想更具体

//GET parameter

//获取参数

$paramValue = $app->request()->get('paramName');

//POST parameter

//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');
});

You can read the documentation on the request object http://docs.slimframework.com/request/variables/

您可以阅读有关请求对象http://docs.slimframework.com/request/variables/的文档

As of Slim v3:

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

回答by vlp

For Slim 3you need to use the method getQueryParams()on the PSR 7 Requestobject.

对于Slim 3,您需要getQueryParams()在 PSR 7Request对象上使用该方法。

Citing the documentation:

引用文档

You can get the query parameters as an associative array on the Request object using getQueryParams().

You can also get a single query parameter value, with optional default value if the parameter is missing, using getQueryParam($key, $default = null).

您可以使用 getQueryParams() 在 Request 对象上以关联数组的形式获取查询参数。

您还可以使用 getQueryParam($key, $default = null) 获取单个查询参数值,如果缺少该参数,则可以使用可选的默认值。

回答by Mulhoon

I fixed my api to receive a json body OR url parameter like this.

我修复了我的 api 以接收这样的 json body 或 url 参数。

$data = json_decode($request->getBody()) ?: $request->params();

This might not suit everyone but it worked for me.

这可能不适合所有人,但对我有用。

回答by Cengkuru Michael

Use $id = $request->getAttribute('id'); //where id is the name of the param

$id = $request->getAttribute('id'); //where id is the name of the param

回答by Tamas Kalman

In Slim 3.0 the following also works:

在 Slim 3.0 中,以下也有效:

routes.php

路由文件

require_once 'user.php';

$app->get('/user/create', '\UserController:create');

user.php

用户名

class UserController
{
    public function create($request, $response, array $args)
    {
        $username = $request->getParam('username'));
        $password = $request->getParam('password'));
        // ...
    }
}

回答by KlevisGjN

IF YOU WANT TO GET PARAMS WITH PARAM NAME

如果您想使用 PARAM NAME 获取 PARAMS

$value = $app->request->params('key');

The params() method will first search PUT variables, then POST variables, then GET variables. If no variables are found, null is returned. If you only want to search for a specific type of variable, you can use these methods instead:

params() 方法将首先搜索 PUT 变量,然后是 POST 变量,然后是 GET 变量。如果未找到变量,则返回 null。如果您只想搜索特定类型的变量,则可以改用以下方法:

//--- GET variable

//--- 获取变量

$paramValue = $app->request->get('paramName');

//--- POST variable

//--- POST 变量

$paramValue = $app->request->post('paramName');

//--- PUT variable

//--- 放置变量

$paramValue = $app->request->put('paramName');

IF YOU WANT TO GET ALL PARAMETERS FROM REQUEST WITHOUT SPECIFYING PARAM NAME, YOU CAN GET ALL OF THEM INTO ARRAY IN FORMAT KEY => VALUE

如果您想在不指定参数名称的情况下从请求中获取所有参数,您可以将它们全部放入格式键 => 值的数组中

$data = json_decode( $app->request->getBody() ) ?: $app->request->params();

$data will be an array that contains all fields from request as below

$data 将是一个包含来自请求的所有字段的数组,如下所示

$data = array(
    'key' => 'value',
    'key' => 'value',
    //...
);

Hope it helps you!

希望对你有帮助!

回答by George P

Not sure much about Slim PHP, but if you want to access the parameters from a URL then you should use the:

对 Slim PHP 不太确定,但如果你想从 URL 访问参数,那么你应该使用:

$_SERVER['QUERY_STRING']

You'll find a bunch of blog posts on Google to solve this. You can also use the PHP function parse_url.

你会在谷歌上找到一堆博客文章来解决这个问题。您还可以使用 PHP 函数parse_url

回答by Smith

Slim 3

修身 3

$request->getQueryParam('page')

or

或者

$app->request->getQueryParam('page')

回答by bkudrle

Probably obvious to most, but just in case, building on vip's answer concerning Slim 3, you can use something like the following to get the values for the parameters.

可能对大多数人来说很明显,但以防万一,建立在 vip 对Slim 3的回答上,您可以使用类似以下内容来获取参数值。

        $logger = $this->getService('logger');
        $params = $request->getQueryParams();
        if ($params)  {
            foreach ($params as $key => $param)     {
                if (is_array($param))   {
                    foreach ($param as $value)  {
                        $logger->info("param[" . $key . "] = " . $value);
                    }
                }
                else    {
                    $logger->info("param[" . $key . "] = " . $param);
                }
            }
        }