Skip to content

Commit

Permalink
优化http的get,post参数获取方式
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed Jul 30, 2018
1 parent ec4bef6 commit 84ef925
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
2 changes: 1 addition & 1 deletion score/Core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
80 changes: 65 additions & 15 deletions score/Core/AppTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' :
Expand All @@ -157,26 +157,69 @@ 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();
}

/**
* getServerParam
* @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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

}

/**
Expand Down
3 changes: 1 addition & 2 deletions score/Core/Memory/AtomicManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,5 @@ public function getAtomicLong(string $name) {
}else{
return null;
}
}

}
}

0 comments on commit 84ef925

Please sign in to comment.