一、getMethod方法,返回当前请求的方法
/**
* 返回当前请求的方法
*/
public function getMethod()
{
if (
isset($_POST[$this->methodParam])
// Never allow to downgrade request from WRITE methods (POST, PATCH, DELETE, etc)
// to read methods (GET, HEAD, OPTIONS) for security reasons.
&& !in_array(strtoupper($_POST[$this->methodParam]), ['GET', 'HEAD', 'OPTIONS'], true)
) {
//通过post方法提交的隐藏字段确定请求方法
return strtoupper($_POST[$this->methodParam]);
}
//请求头中获取请求方法
if ($this->headers->has('X-Http-Method-Override')) {
return strtoupper($this->headers->get('X-Http-Method-Override'));
}
// REQUEST_METHOD字段确定请求方法
if (isset($_SERVER['REQUEST_METHOD'])) {
return strtoupper($_SERVER['REQUEST_METHOD']);
}
//默认GET
return 'GET';
}
二、一些方法
- getIsGet 是否GET请求
- getIsOptions 是否OPTIONS请求
- getIsHead 是否HEAD请求
- getIsPost 是否POST请求
- getIsDelete 是否DELETE请求
- getIsPut 是否PUT请求
- getIsPatch 是否PATCH请求
- getIsAjax 是否Ajax请求
- getIsPjax 是否Pjax请求
- getIsFlash 是否Adobe Flash or Flex请求
// 是否GET请求
public function getIsGet()
{
return $this->getMethod() === 'GET';
}
// 是否OPTIONS请求
public function getIsOptions()
{
return $this->getMethod() === 'OPTIONS';
}
// 是否HEAD请求
public function getIsHead()
{
return $this->getMethod() === 'HEAD';
}
// 是否POST请求
public function getIsPost()
{
return $this->getMethod() === 'POST';
}
// 是否DELETE请求
public function getIsDelete()
{
return $this->getMethod() === 'DELETE';
}
// 是否PUT请求
public function getIsPut()
{
return $this->getMethod() === 'PUT';
}
// 是否PATCH请求
public function getIsPatch()
{
return $this->getMethod() === 'PATCH';
}
// 是否Ajax请求
public function getIsAjax()
{
return $this->headers->get('X-Requested-With') === 'XMLHttpRequest';
}
// 是否Pjax请求
public function getIsPjax()
{
return $this->getIsAjax() && $this->headers->has('X-Pjax');
}
// 是否Adobe Flash or Flex请求
public function getIsFlash()
{
$userAgent = $this->headers->get('User-Agent', '');
return stripos($userAgent, 'Shockwave') !== false
|| stripos($userAgent, 'Flash') !== false;
}