为PUT或DELETE请求覆盖$ _POST [英] Overwriting $_POST for PUT or DELETE requests
问题描述
在PHP中,我希望能够全局访问PUT
和DELETE
变量,类似于全局访问GET
和POST
变量的方式.我最初考虑将数据分别添加到全局命名空间中的$_PUT
和$_DELETE
中,但是后来我意识到每个请求的数据都存储在消息主体中,因此,从一个消息集中获取一个以上的数据集是不可能的. POST
,PUT
或DELETE
请求.
In PHP I would like to be able to access PUT
and DELETE
vars globally similar to how GET
and POST
vars are accessed globally. I originally considered adding the data to $_PUT
and $_DELETE
respectively in the global namespace, but then I realized that the data for each request is stored in the message body so there's no way for there to be more than one dataset from a POST
, PUT
, or DELETE
request.
即str_parse( file_get_contents( 'php://input' ), $_POST );
我是不是很傻,还是有更好的方法来访问PUT
和DELETE
数据?
Am I being silly, or is there a better way to access PUT
and DELETE
data?
编辑以阐明我的想法:
我非常了解$_POST
中的数据来源,实际上我在前面的问题中提到了它.如果将HTTP POST请求发送到服务器,则数据存储在php://input
中.如果将HTTP PUT或DELETE请求发送到服务器,则数据将存储在完全相同的位置,这意味着$_POST
将为空(因为尽管有数据,但没有数据POSTed
.
I am very well aware of the source of the data in $_POST
, in fact i mentioned it earlier in my question. If a HTTP POST request is sent to the server the data is stored in php://input
. If a HTTP PUT or DELETE request is sent to the server, the data is stored in the exact same place, meaning that $_POST
will be empty (as no data was POSTed
despite data being available.
另一方面,GET
请求是通过查询字符串传递的.这允许同时传递$_POST
和$_GET
变量. 不可能同时传递POST
和PUT or DELETE
变量.
A GET
request, on the other hand, is passed via the query string. This allows simultaneous passing of $_POST
and $_GET
variables. It is not possible to simultaneously pass POST
and PUT or DELETE
variables.
如果我在PUT
和/或DELETE
请求上从php://input
覆盖$_POST
,则不会丢失数据.
If I overwrite $_POST
from php://input
on PUT
and or DELETE
requests, there is no data loss.
添加的替代方法:
global $_PUT;
global $_DELETE;
函数的开头似乎很愚蠢,因为无论如何我一次只能使用一个.
to the beginning of functions seems silly, as I'll only be able to use one at a time anyway.
我要回答的第一个问题是覆盖$_POST
中存在哪些副作用或问题.我不可能是第一个尝试像这样愚蠢的人的人:
My first question, which is the one I really want answered, is about what side-effects or issues exist in overwriting $_POST
. I can't possibly be the first person to try something as silly as:
$_POST['foo'] = 'bar';
我只是担心,如果我做类似的事情,它可能不会在示波器中保留.
I'm just concerned that if I do anything similar that it might not be preserved across scopes.
推荐答案
您会在整个互联网上看到这种称为坏习惯"的信息,但是如果您真正了解为什么,它就是不好的做法",那么答案就变得模糊了.最具体的原因是经常遭到公交车撞"的情况-如果项目移交给新的开发人员怎么办?
You'll see this called "bad practice" all over the internet, but if you really get in to why it is "bad practice", well, the answers get fuzzy. The most concrete reason is the "hit by a bus" scenario so often bandied about - what if the project gets handed off to a new developer?
绞尽脑汁(毕竟您可以发表评论),确实没有令人信服的理由不这样做,但是同样,没有令人信服的理由来去做.如果要使这些值成为全局值,为什么不将它们放在$_SESSION
键中?还是做一个全局变量?还是创建一个静态类来访问PUT/DELETE值?使用所有其他可选方法,我认为覆盖$_POST
虽然不会使您的服务器爆炸,但最有可能使您头痛.
Hand wringing aside (you can leave comments, after all), there really isn't a compelling reason not to do it like this, but again, there isn't a compelling reason to do it, either. Why not put the values in a $_SESSION
key if you want them global? Or make a global variable? Or make a static class to access the PUT/DELETE values through? With all the other optional approaches, I think that overwriting $_POST
, while it won't make your server explode, is the most likely to cause you a headache down the road.
我把这个小的静态类放在一起,您需要在依赖它之前对其进行测试.使用:
I threw this little static class together, you'll want to test this out before relying on it. Use:
//To check if this is a rest request:
Rest::isRest();
//To get a parameter from PUT
$put_var = Rest::put('keyname', false);
//To get a parameter from DELETE
$dele_var = Rest::delete('keyname', false);
class Rest {
static private $put = false;
static private $delete = false;
static private $is_rest = false;
function __construct() {
self::$is_rest = true;
switch ($_SERVER['REQUEST_METHOD']) {
case 'PUT':
parse_str(self::getVars(), self::$put);
break;
case 'DELETE':
parse_str(self::getVars(), self::$delete);
break;
default:
self::$is_rest = false;
}
}
private static function getVars() {
if (strlen(trim($vars = file_get_contents('php://input'))) === 0)
$vars = false;
return $vars;
}
public static function delete($key=false, $default=false) {
if (self::$is_rest !== true)
return $default;
if (is_array(self::$delete) && array_key_exists($key, self::$delete))
return self::$delete[$key];
return $default;
}
public static function put($key=false, $default=false) {
if (self::$is_rest !== true)
return $default;
if (is_array(self::$put) && array_key_exists($key, self::$put))
return self::$put[$key];
return $default;
}
public static function isRest() {
return self::$is_rest;
}
}
这篇关于为PUT或DELETE请求覆盖$ _POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!