首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony getMethod()与getRealMethod()

Symfony getMethod()与getRealMethod()
EN

Stack Overflow用户
提问于 2014-06-09 07:12:14
回答 1查看 836关注 0票数 3

我知道symfony api解释了getMethod()获取请求“预期”方法,而getRealMethod()获得“真实”请求方法,但我不知道“意图”和“真实”是什么意思。有人能告诉我吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-06-09 07:22:37

getRealMethod()返回真实的请求方法,而getMethod()返回预期的请求方法,这意味着真正的请求方法是POST,但symfony与DELETE类似。

见下面的例子:

代码语言:javascript
复制
<form method="post" action="..." >
  <input type="hidden" name="_method" value="DELETE" />
  ...
</form>

真正的请求方法是POST,而getMethod()将返回DELETE

检查来源:

代码语言:javascript
复制
/**
 * 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'));
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24115345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档