From 84ef925a473ca41a60e60a7593538a61d37670a1 Mon Sep 17 00:00:00 2001 From: bingcool <2437667702@qq.com> Date: Tue, 31 Jul 2018 00:11:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96http=E7=9A=84get,post?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- score/Core/App.php | 2 +- score/Core/AppTrait.php | 80 +++++++++++++++++++++++------ score/Core/Memory/AtomicManager.php | 3 +- 3 files changed, 67 insertions(+), 18 deletions(-) diff --git a/score/Core/App.php b/score/Core/App.php index 829a8daf..83b9521a 100755 --- a/score/Core/App.php +++ b/score/Core/App.php @@ -85,7 +85,7 @@ protected function init() { * boostrap 初始化引导 */ protected function bootstrap() { - Swfy::$config['application_index']::bootstrap($this->getRequestParam()); + Swfy::$config['application_index']::bootstrap($this->getRequestParams()); } /** diff --git a/score/Core/AppTrait.php b/score/Core/AppTrait.php index 2b339e3c..d9dbce37 100755 --- a/score/Core/AppTrait.php +++ b/score/Core/AppTrait.php @@ -126,12 +126,12 @@ public function getResponse() { } /** - * getRequestParam + * getRequestParam 获取请求参数,包括get,post * @param string $key * @param string $mothod * @return mxixed */ - public function getRequestParam(string $name = null) { + public function getRequestParams(string $name = null) { $mothod = strtolower($this->getMethod()); switch($mothod) { case 'get' : @@ -157,18 +157,61 @@ public function getRequestParam(string $name = null) { return $value; } + /** + * getQueryParams 获取get参数 + * @param string|null $name + * @return string + */ + public function getQueryParams(string $name = null) { + $input = $this->request->get; + if($name) { + $value = (isset($input[$name]) && !empty($input[$name])) ? $input[$name] : null; + }else { + $value = isset($input) ? $input : []; + + } + return $value; + + } + + /** + * getPostParmams 获取Post参数 + * @param string|null $name + * @return mixed + */ + public function getPostParmams(string $name = null) { + $input = $this->request->post; + if($name) { + $value = (isset($input[$name]) && !empty($input[$name])) ? $input[$name] : null; + }else { + $value = isset($input) ? $input : []; + + } + return $value; + } + /** * getCookieParam * @param string|null $name * @return mixed */ - public function getCookieParam(string $name = null) { + public function getCookieParams(string $name = null) { + $cookies = $this->request->cookie; if($name) { - $value = isset($this->request->cookie[$name]) ? $this->request->cookie[$name] : null; - return $value; + $value = isset($cookies[$name]) ? $cookies[$name] : null; + }else { + $value = isset($cookies) ? $cookies : []; + } + return $value; + } - return $this->request->cookie; + /** + * getData 获取完整的原始Http请求报文,包括Http Header和Http Body + * @return string + */ + public function getData() { + return $this->request->getData(); } /** @@ -176,7 +219,7 @@ public function getCookieParam(string $name = null) { * @param string|null $name * @return mixed */ - public function getServerParam(string $name = null) { + public function getServerParams(string $name = null) { if($name) { $value = isset($this->request->server[$name]) ? $this->request->server[$name] : null; return $value; @@ -190,7 +233,7 @@ public function getServerParam(string $name = null) { * @param string|null $name * @return mixed */ - public function getHeaderParam(string $name = null) { + public function getHeaderParams(string $name = null) { if($name) { $value = isset($this->request->header[$name]) ? $this->request->header[$name] : null; return $value; @@ -279,7 +322,7 @@ public function getHomeUrl(bool $ssl=false) { * @param boolean $ssl * @return void */ - public function rememberUrl(string $name = null, string $url=null, bool $ssl=false) { + public function rememberUrl(string $name = null, string $url = null, bool $ssl = false) { if($url && $name) { $this->previousUrl[$name] = $url; }else { @@ -483,15 +526,14 @@ public function parseUri(string $url) { * @param int $code default 301 * @return void */ - public function redirect(string $url, array $params = [], int $code=301) { + public function redirect(string $url, array $params = [], int $code = 301) { $query_string = ''; trim($url); if(strpos($url, 'http') === false || strpos($url, 'https') === false) { if(strpos($url, '/') != 0) { $url = '/'.$url; } - } - + } if($params) { if(strpos($url,'?') > 0) { foreach($params as $name=>$value) { @@ -506,9 +548,17 @@ public function redirect(string $url, array $params = [], int $code=301) { $query_string = rtrim($query_string,'&'); } } - $this->status($code); - $this->response->header('Location', $url.$query_string); - return; + // 发送Http跳转,调用此方法会自动end发送并结束响应,2.2.0+版本支持 + if(version_compare(swoole_version(), '2.2.0', '>')) { + $this->response->redirect($url.$query_string, $code); + return; + }else { + $this->status($code); + $this->response->header('Location', $url.$query_string); + $this->response->end(); + return; + } + } /** diff --git a/score/Core/Memory/AtomicManager.php b/score/Core/Memory/AtomicManager.php index 8b129728..7d9a66d7 100644 --- a/score/Core/Memory/AtomicManager.php +++ b/score/Core/Memory/AtomicManager.php @@ -66,6 +66,5 @@ public function getAtomicLong(string $name) { }else{ return null; } - } - + } } \ No newline at end of file