mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 23:52:49 +08:00
改进Request类 增加param has only except方法 去掉 get post delete put方法
This commit is contained in:
@@ -56,10 +56,10 @@ class Request
|
|||||||
*/
|
*/
|
||||||
protected $dispatch = [];
|
protected $dispatch = [];
|
||||||
|
|
||||||
protected $get = [];
|
/**
|
||||||
protected $post = [];
|
* @var array 请求参数
|
||||||
protected $put = [];
|
*/
|
||||||
protected $delete = [];
|
protected $param = [];
|
||||||
protected $file = [];
|
protected $file = [];
|
||||||
protected $cookie = [];
|
protected $cookie = [];
|
||||||
protected $server = [];
|
protected $server = [];
|
||||||
@@ -155,19 +155,19 @@ class Request
|
|||||||
$info['path'] = '/';
|
$info['path'] = '/';
|
||||||
}
|
}
|
||||||
$options = [];
|
$options = [];
|
||||||
$options[strtolower($method)] = $params;
|
$options['param'] = $params;
|
||||||
$queryString = '';
|
$queryString = '';
|
||||||
if (isset($info['query'])) {
|
if (isset($info['query'])) {
|
||||||
parse_str(html_entity_decode($info['query']), $query);
|
parse_str(html_entity_decode($info['query']), $query);
|
||||||
if (isset($options['get'])) {
|
if (!empty($options['param'])) {
|
||||||
$options['get'] = array_replace($query, $options['get']);
|
$options['param'] = array_replace($query, $options['param']);
|
||||||
$queryString = http_build_query($query, '', '&');
|
$queryString = http_build_query($query, '', '&');
|
||||||
} else {
|
} else {
|
||||||
$options['get'] = $query;
|
$options['param'] = $query;
|
||||||
$queryString = $info['query'];
|
$queryString = $info['query'];
|
||||||
}
|
}
|
||||||
} elseif (isset($options['get'])) {
|
} elseif (isset($options['param'])) {
|
||||||
$queryString = http_build_query($options['get'], '', '&');
|
$queryString = http_build_query($options['param'], '', '&');
|
||||||
}
|
}
|
||||||
$server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
|
$server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
|
||||||
$server['QUERY_STRING'] = $queryString;
|
$server['QUERY_STRING'] = $queryString;
|
||||||
@@ -350,61 +350,92 @@ class Request
|
|||||||
*/
|
*/
|
||||||
public function param($name = '')
|
public function param($name = '')
|
||||||
{
|
{
|
||||||
|
if (empty($this->param)) {
|
||||||
$method = $this->method();
|
$method = $this->method();
|
||||||
return $this->$method($name);
|
// 自动获取请求变量
|
||||||
|
switch ($method) {
|
||||||
|
case 'POST':
|
||||||
|
$vars = Input::post();
|
||||||
|
break;
|
||||||
|
case 'PUT':
|
||||||
|
$vars = Input::put();
|
||||||
|
break;
|
||||||
|
case 'DELETE':
|
||||||
|
$vars = Input::delete();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$vars = [];
|
||||||
|
}
|
||||||
|
// 当前请求参数和URL地址中的参数合并
|
||||||
|
$this->param = array_merge(Input::get(), $vars);
|
||||||
|
}
|
||||||
|
if ($name) {
|
||||||
|
return isset($this->param[$name]) ? $this->param[$name] : null;
|
||||||
|
} else {
|
||||||
|
return $this->param;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前请求的get参数
|
* 是否存在某个请求参数
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $name 变量名
|
* @param string $name 变量名
|
||||||
|
* @param bool $checkEmpty 是否检测空值
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get($name = '')
|
public function has($name, $checkEmpty = false)
|
||||||
{
|
{
|
||||||
return Input::data($this->get ?: $_GET, $name);
|
if (empty($this->param)) {
|
||||||
|
$param = $this->param();
|
||||||
|
} else {
|
||||||
|
$param = $this->param;
|
||||||
|
}
|
||||||
|
if (isset($this->param[$name])) {
|
||||||
|
return ($checkEmpty && '' === $this->param[$name]) ? false : true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前请求的post参数
|
* 获取指定的参数
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $name 变量名
|
* @param string|array $name 变量名
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function post($name = '')
|
public function only($name)
|
||||||
{
|
{
|
||||||
return Input::data($this->post ?: $_POST, $name);
|
if (is_string($name)) {
|
||||||
|
return $this->param($name);
|
||||||
|
} else {
|
||||||
|
foreach ($name as $key) {
|
||||||
|
$item[$key] = $this->param($name);
|
||||||
|
}
|
||||||
|
return $item;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前请求的put参数
|
* 排除指定参数获取
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $name 变量名
|
* @param string|array $name 变量名
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function put($name = '')
|
public function except($name)
|
||||||
{
|
{
|
||||||
static $_PUT = null;
|
$param = $this->param();
|
||||||
if (is_null($_PUT)) {
|
if (is_string($name)) {
|
||||||
parse_str(file_get_contents('php://input'), $_PUT);
|
if (isset($param[$name])) {
|
||||||
|
unset($param[$name]);
|
||||||
}
|
}
|
||||||
return Input::data($this->put ?: $_PUT, $name);
|
} else {
|
||||||
|
foreach ($param as $key => $val) {
|
||||||
|
if (in_array($key, $name)) {
|
||||||
|
unset($param[$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 当前请求的delete参数
|
|
||||||
* @access public
|
|
||||||
* @param string $name 变量名
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function delete($name = '')
|
|
||||||
{
|
|
||||||
static $_DELETE = null;
|
|
||||||
if (is_null($_DELETE)) {
|
|
||||||
parse_str(file_get_contents('php://input'), $_DELETE);
|
|
||||||
$_DELETE = array_merge($_DELETE, $_GET);
|
|
||||||
}
|
}
|
||||||
return Input::data($this->delete ?: $_DELETE, $name);
|
}
|
||||||
|
return $param;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user