增加在Request中获取get、post值的功能

This commit is contained in:
TOP糯米 2019-07-19 15:42:58 +08:00
parent ce86ace205
commit b94686b976
2 changed files with 104 additions and 0 deletions

View File

@ -1,5 +1,23 @@
<?php <?php
/**
* 过滤数组
* @param array $array
* @param array $except
* @param string $filter
* @param array $result
*/
function filterArray($array = [], $except = [], $filter = 'filter', &$result = [])
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$this->processArray($value, $result[$key]);
} else {
$result[$key] = (in_array($key, $except) || !$filter) ? $value : $filter($value);
}
}
}
/** /**
* 调用请求类 * 调用请求类
*/ */

View File

@ -4,6 +4,7 @@ namespace top\library\http;
use top\decorator\ifs\DecoratorIfs; use top\decorator\ifs\DecoratorIfs;
use top\decorator\InitDecorator; use top\decorator\InitDecorator;
use top\library\exception\RequestException;
use top\library\Register; use top\library\Register;
use top\library\route\driver\Command; use top\library\route\driver\Command;
use top\library\route\driver\Pathinfo; use top\library\route\driver\Pathinfo;
@ -64,6 +65,12 @@ class Request
*/ */
private $params = []; private $params = [];
/**
* post、get数据移除的值
* @var array
*/
private $except = [];
/** /**
* @return null|Request * @return null|Request
*/ */
@ -340,6 +347,85 @@ class Request
return $data; return $data;
} }
/**
* 移除值
* @param $field
* @return $this
*/
public function except($field = null)
{
if (is_array($field)) {
$this->except = array_merge($field, $this->except);
} elseif ($field) {
$this->except[] = $field;
}
return $this;
}
/**
* GET数据
* @param null $name
* @param array $except
* @param string $filter
* @return null
*/
public function get($name = null, $except = [], $filter = 'filter')
{
return $this->requestData('get', $name, $except, $filter);
}
/**
* POST数据
* @param null $name
* @param array $except
* @param string $filter
* @return null
*/
public function post($name = null, $except = [], $filter = 'filter')
{
return $this->requestData('post', $name, $except, $filter);
}
/**
* GET POST公共方法
* @param $type
* @param $name
* @param $except
* @param $filter
* @return null
*/
private function requestData($type, $name, $except, $filter)
{
$data = ($type == 'get') ? $_GET : $_POST;
$name = ($name == '*') ? null : $name;
// 过滤数组
if (!is_array($except)) {
$except = [$except];
}
filterArray($data, $except, $filter, $data);
// 移除指定的值
foreach ($this->except as $key => $value) {
if (isset($data[$value])) {
unset($data[$value]);
}
}
// 重置except的值
$this->except = [];
if ($name) {
if (isset($data[$name])) {
return $data[$name];
} else {
return null;
}
} else {
return $data;
}
}
public function __destruct() public function __destruct()
{ {
} }