From b94686b9767dcafa23080644e09daff6c86cf42d Mon Sep 17 00:00:00 2001 From: topnuomi <1130395124@qq.com> Date: Fri, 19 Jul 2019 15:42:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9C=A8Request=E4=B8=AD?= =?UTF-8?q?=E8=8E=B7=E5=8F=96get=E3=80=81post=E5=80=BC=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/library/functions/functions.php | 18 +++++ framework/library/http/Request.php | 86 +++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/framework/library/functions/functions.php b/framework/library/functions/functions.php index f0a9ad8..8ce8368 100644 --- a/framework/library/functions/functions.php +++ b/framework/library/functions/functions.php @@ -1,5 +1,23 @@ $value) { + if (is_array($value)) { + $this->processArray($value, $result[$key]); + } else { + $result[$key] = (in_array($key, $except) || !$filter) ? $value : $filter($value); + } + } +} + /** * 调用请求类 */ diff --git a/framework/library/http/Request.php b/framework/library/http/Request.php index 660f7a2..5d6f219 100644 --- a/framework/library/http/Request.php +++ b/framework/library/http/Request.php @@ -4,6 +4,7 @@ namespace top\library\http; use top\decorator\ifs\DecoratorIfs; use top\decorator\InitDecorator; +use top\library\exception\RequestException; use top\library\Register; use top\library\route\driver\Command; use top\library\route\driver\Pathinfo; @@ -64,6 +65,12 @@ class Request */ private $params = []; + /** + * post、get数据移除的值 + * @var array + */ + private $except = []; + /** * @return null|Request */ @@ -340,6 +347,85 @@ class Request 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() { }