Here is some code which may be helpful for others wanting to handle PUT and DELETE params. You are able to set $_PUT and $_DELETE via $GLOBALS[], but they will not be directly accessible in functions unless declared global or accessed via $GLOBALS[]. To get around this, I've made a simple class for reading GET/POST/PUT/DELETE request arguments. This also populates $_REQUEST with PUT/DELETE params.
This class will parse the PUT/DELETE params and support GET/POST as well.
class Params {
private $params = Array();
public function __construct() {
$this->_parseParams();
}
/**
* @brief Lookup request params
* @param string $name Name of the argument to lookup
* @param mixed $default Default value to return if argument is missing
* @returns The value from the GET/POST/PUT/DELETE value, or $default if not set
*/
public function get($name, $default = null) {
if (isset($this->params[$name])) {
return $this->params[$name];
} else {
return $default;
}
}
private function _parseParams() {
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "PUT" || $method == "DELETE") {
parse_str(file_get_contents('php://input'), $this->params);
$GLOBALS["_{$method}"] = $this->params;
// Add these request vars into _REQUEST, mimicing default behavior, PUT/DELETE will override existing COOKIE/GET vars
$_REQUEST = $this->params + $_REQUEST;
} else if ($method == "GET") {
$this->params = $_GET;
} else if ($method == "POST") {
$this->params = $_POST;
}
}
}
5 个回复
安正超
赞同来自: admin 、entere
1. 请求方式
2. 使用:
3. 请求格式请确定为:
我的测试例子:
路由:
请求:
请求体:
能正常使用
JohnLui
赞同来自:
FiveSay - 成武
赞同来自:
entere
赞同来自:
http delete请求,注意这个http 请求不是由laravel表单发起的,是一个http的delete请求,有可能是任何一个客户端发起的。
用laravel 框架接收这个http delete请求,我们知道,如果是一个get 或post 用Input::get() 就可以获取到,http delete 请求的参数 在laravel框架中 如何获取呢?
一一逍遥
赞同来自:
所以你就得想其他的方式了,分析一下PHP的$_SERVER这个全局变量然后结合file_get_contents('php://input');这个方法。
http://php.net/manual/zh/reser ... r.php
楼上说用 Input::getContent() 这个方法,好像我没找到这个方法。
Here is some code which may be helpful for others wanting to handle PUT and DELETE params. You are able to set $_PUT and $_DELETE via $GLOBALS[], but they will not be directly accessible in functions unless declared global or accessed via $GLOBALS[]. To get around this, I've made a simple class for reading GET/POST/PUT/DELETE request arguments. This also populates $_REQUEST with PUT/DELETE params.
This class will parse the PUT/DELETE params and support GET/POST as well.