我知道 symfony api 解释 getMethod() 获取请求“预期”方法,而 getRealMethod() 获取“真实”请求方法,但我无法弄清楚“预期”和“真实”是什么意思。谁能告诉我?谢谢
问问题
757 次
1 回答
5
getRealMethod()
返回真正的请求方法,同时getMethod()
返回预期的请求方法,这意味着真正的请求方法POST
只是 symfony 像其他人一样对待DELETE
。
请参见下面的示例:
<form method="post" action="..." >
<input type="hidden" name="_method" value="DELETE" />
...
</form>
真正的请求方法是POST
,whilegetMethod()
会返回DELETE
。
检查来源:
/**
* Gets the request "intended" method.
*
* If the X-HTTP-Method-Override header is set, and if the method is a POST,
* then it is used to determine the "real" intended HTTP method.
*
* The _method request parameter can also be used to determine the HTTP method,
* but only if enableHttpMethodParameterOverride() has been called.
*
* The method is always an uppercased string.
*
* @return string The request method
*
* @api
*
* @see getRealMethod
*/
public function getMethod()
{
if (null === $this->method) {
$this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
if ('POST' === $this->method) {
if ($method = $this->headers->get('X-HTTP-METHOD-OVERRIDE')) {
$this->method = strtoupper($method);
} elseif (self::$httpMethodParameterOverride) {
$this->method = strtoupper($this->request->get('_method', $this->query->get('_method', 'POST')));
}
}
}
return $this->method;
}
/**
* Gets the "real" request method.
*
* @return string The request method
*
* @see getMethod
*/
public function getRealMethod()
{
return strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
}
于 2014-06-09T07:22:37.540 回答