支持表单模拟ajax和pjax请求 增加var_ajax var_pjax 配置参数

This commit is contained in:
thinkphp
2016-09-25 19:31:06 +08:00
parent b30db5c754
commit f5f9fcfb92
2 changed files with 21 additions and 5 deletions

View File

@@ -95,6 +95,10 @@ return [
'url_controller_layer' => 'controller',
// 表单请求类型伪装变量
'var_method' => '_method',
// 表单ajax伪装变量
'var_ajax' => '_ajax',
// 表单pjax伪装变量
'var_pjax' => '_pjax',
// +----------------------------------------------------------------------
// | 模板设置

View File

@@ -1186,22 +1186,34 @@ class Request
/**
* 当前是否Ajax请求
* @access public
* @param bool $ajax true 获取原始ajax请求
* @return bool
*/
public function isAjax()
public function isAjax($ajax = false)
{
$value = $this->server('HTTP_X_REQUESTED_WITH');
return (!is_null($value) && strtolower($value) == 'xmlhttprequest') ? true : false;
$value = $this->server('HTTP_X_REQUESTED_WITH', '', 'strtolower');
$result = ('xmlhttprequest' == $value) ? true : false;
if (true === $ajax) {
return $result;
} else {
return $this->param(Config::get('var_ajax')) ? true : $result;
}
}
/**
* 当前是否Pjax请求
* @access public
* @param bool $pjax true 获取原始pjax请求
* @return bool
*/
public function isPjax()
public function isPjax($pjax = false)
{
return !is_null($this->server('HTTP_X_PJAX')) ? true : false;
$result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
if (true === $pjax) {
return $result;
} else {
return $this->param(Config::get('var_pjax')) ? true : $result;
}
}
/**