Browse Source

增加 admin_exist 函数,用于代替 exit

jqh 4 years ago
parent
commit
5302bf6f85
2 changed files with 35 additions and 0 deletions
  1. 20 0
      src/Admin.php
  2. 15 0
      src/Support/helpers.php

+ 20 - 0
src/Admin.php

@@ -20,9 +20,11 @@ use Illuminate\Auth\GuardHelpers;
 use Illuminate\Contracts\Auth\Authenticatable;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Http\Exceptions\HttpResponseException;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Event;
 use Illuminate\Support\Str;
+use Symfony\Component\HttpFoundation\Response;
 
 class Admin
 {
@@ -364,6 +366,24 @@ class Admin
         return app('admin.extend');
     }
 
+    /**
+     * 响应并中断后续逻辑.
+     *
+     * @param Response|string|array $response
+     *
+     * @throws HttpResponseException
+     */
+    public static function exit($response = '')
+    {
+        if (is_array($response)) {
+            $response = response()->json($response);
+        } elseif ($response instanceof JsonResponse) {
+            $response = $response->send();
+        }
+
+        throw new HttpResponseException($response instanceof Response ? $response : response($response));
+    }
+
     /**
      * 类自动加载器.
      *

+ 15 - 0
src/Support/helpers.php

@@ -5,6 +5,7 @@ use Dcat\Admin\Support\Helper;
 use Illuminate\Contracts\Support\Htmlable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\MessageBag;
+use Symfony\Component\HttpFoundation\Response;
 
 if (! function_exists('admin_setting')) {
     /**
@@ -568,3 +569,17 @@ if (! function_exists('admin_javascript_json')) {
         return Dcat\Admin\Support\JavaScript::format($data);
     }
 }
+
+if (! function_exists('admin_exit')) {
+    /**
+     * 响应并中断后续逻辑.
+     *
+     * @param Response|string|array $response
+     *
+     * @throws \Illuminate\Http\Exceptions\HttpResponseException
+     */
+    function admin_exit($response = '')
+    {
+        Admin::exit($response);
+    }
+}