gpt4 book ai didi

php - 使用 Slim 测试 PUT 参数值的问题

转载 作者:搜寻专家 更新时间:2023-10-31 22:04:23 24 4
gpt4 key购买 nike

我不完全理解它与 PUT 变量和 Slim 的关系。

请考虑以下路线:

$app->put('/users/me',function() use ($app){
$app->response->headers->set('Content-Type', 'application/json');
$userData = $app->request()->put();
var_dump($userData);
});

这个路由基本上接受一个 PUT 请求并返回它附带的任何变量。当我用 curl 测试它时,它似乎按预期工作:

oris@oris:~/slim-api$ curl -i -H "Accept: application/json" -X PUT -d "hello=there" http://slim-api.com/users/me
HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Thu, 06 Feb 2014 09:53:11 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.6-1ubuntu1.5
Set-Cookie: PHPSESSID=2r6ta2fg6vdk7; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

array(1) {
["hello"]=>
string(5) "there"
}

我用 bootstrap.php file 为这条路线写了一点 PHPUnit 测试:

 public function testPutUsersMe()
{
$this->put('/users/me',array("myKey=myValue"));
$userResponse = $this->response->body();
var_dump($userResponse);
}

我还向这个引导文件添加了一个小的附加项,以根据每个请求更新超全局变量:

//sets php super globals so other packages would have access to them on tests context
public function setRequestGlobals($method,$path,$options = array())
{
$_SERVER['REQUEST_METHOD'] = $method;
if (!empty($options)){
if ($method === "GET"){
foreach($options as $key => $value){
$_GET[$key] = $value;
}
}
if ($method === "POST"){
foreach($options as $key => $value){
$_POST[$key] = $value;
}
}
if ($method === "PUT"){
foreach($options as $key => $value){
$_POST[$key] = $value;
}
}
}

}

我在 request 方法中调用了 $this->setRequestGlobals($method,$path,$options);。但这会导致以下不太正确的响应:

oris@oris:~/slim-api$ phpunit
PHPUnit 3.7.29 by Sebastian Bergmann.

Configuration read from /home/oris/slim-api/phpunit.xml

...........string(44) "Array
(
[slim.input] => myKey=myValue
)
"


Time: 119 ms, Memory: 7.75Mb

OK (11 tests, 85 assertions)
oris@oris:~/slim-api$

我所说的“不太正确”的意思是 [slim.input] 现在是参数值,键和值是值。如果我将路由(和测试)方法更改为 POST,我将获得预期的输出:

oris@oris:~/slim-api$ phpunit
PHPUnit 3.7.29 by Sebastian Bergmann.

Configuration read from /home/oris/slim-api/phpunit.xml

...........string(33) "Array
(
[myKey] => myValue
)
"


Time: 128 ms, Memory: 7.75Mb

OK (11 tests, 85 assertions)
oris@oris:~/slim-api$

问题:

  • 通常,我如何将带有 curl 的数组传递给此路由,而不是 flat key1=value1, key2=value2 ?

  • 当唯一改变的是请求方法时,为什么测试输出中会发生这种变化?

奇怪的是,当我深入研究 Slim 代码时,在 /vendor/slim/slim/Slim/Http/Request.php 上,我看到 put 函数只是 post 的别名:

    /**
* Fetch PUT data (alias for \Slim\Http\Request::post)
* @param string $key
* @param mixed $default Default return value when key does not exist
* @return array|mixed|null
*/
public function put($key = null, $default = null)
{
return $this->post($key, $default);
}

最佳答案

据我所知,slim 需要先配置 header 以接受 'PUT' 方法或者您需要将 '_METHOD=PUT' 的参数与数据数组一起添加,并将请求作为 'POST' 发送。传递的参数可能如下所示:array("_METHOD"=>"PUT","data"=>array("myKey"=>"myValue"))

关于php - 使用 Slim 测试 PUT 参数值的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21659664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com