优化baseController,新增跳转方法 ;

This commit is contained in:
augushong
2020-12-06 22:20:39 +08:00
parent 0b7dbe7163
commit 704337eb20

View File

@@ -106,20 +106,7 @@ abstract class BaseController
public function success($msg = '操作成功', $jump_to_url = null, $code = 200, $params = [])
{
if (is_null($jump_to_url)) {
$jump_to_url = \request()->server('HTTP_REFERER');
} else {
if ($jump_to_url instanceof Url) {
$jump_to_url = (string)$jump_to_url;
} else {
if (strpos($jump_to_url, 'http') !== 0) {
$jump_to_url = url($jump_to_url);
}
}
}
$jump_to_url = $this->parseJumpUrl($jump_to_url);
$data = [
'msg' => $msg,
'jump_to_url' => $jump_to_url,
@@ -140,18 +127,7 @@ abstract class BaseController
public function error($msg = '操作失败', $jump_to_url = null, $code = 200, $params = [])
{
if (is_null($jump_to_url)) {
$jump_to_url = \request()->server('HTTP_REFERER');
} else {
if ($jump_to_url instanceof Url) {
$jump_to_url = (string)$jump_to_url;
} else {
if (strpos($jump_to_url, 'http') !== 0) {
$jump_to_url = url($jump_to_url);
}
}
}
$jump_to_url = $this->parseJumpUrl($jump_to_url);
$data = [
'msg' => $msg,
@@ -170,4 +146,27 @@ abstract class BaseController
View::assign($data);
throw new HttpResponseException(response(View::fetch('common@tpl/error'), $code));
}
public function redirect($jump_to_url, $code = 302)
{
$jump_to_url = $this->parseJumpUrl($jump_to_url);
throw new HttpResponseException(redirect($jump_to_url), $code);
}
public function parseJumpUrl($jump_to_url)
{
if (is_null($jump_to_url)) {
$jump_to_url = \request()->server('HTTP_REFERER');
} else {
if ($jump_to_url instanceof Url) {
$jump_to_url = $jump_to_url;
} else {
$jump_to_url = url($jump_to_url);
}
}
return (string)$jump_to_url;
}
}