浏览代码

Form::currentUrl

jqh 4 年之前
父节点
当前提交
fd7efbe8ac
共有 3 个文件被更改,包括 33 次插入14 次删除
  1. 2 0
      src/Form.php
  2. 28 12
      src/Traits/HasFormResponse.php
  3. 3 2
      src/Widgets/Form.php

+ 2 - 0
src/Form.php

@@ -103,6 +103,8 @@ class Form implements Renderable
      */
     const REMOVE_FLAG_NAME = '_remove_';
 
+    const CURRENT_URL_NAME = '_current_';
+
     /**
      * Available fields.
      *

+ 28 - 12
src/Traits/HasFormResponse.php

@@ -9,6 +9,8 @@ use Illuminate\Validation\Validator;
 
 trait HasFormResponse
 {
+    protected $currentUrl;
+
     /**
      * Get ajax response.
      *
@@ -25,18 +27,14 @@ trait HasFormResponse
         bool $status = true,
         array $options = []
     ) {
-        if ($this->isAjaxRequest()) {
-            $location = $options['location'] ?? false;
-            $urlKey = $location ? 'location' : 'redirect';
-
-            return response()->json([
-                'status'   => $status,
-                'message'  => $message,
-                $urlKey    => $redirect ? admin_url($redirect) : '',
-            ]);
-        }
+        $location = $options['location'] ?? false;
+        $urlKey = $location ? 'location' : 'redirect';
 
-        return false;
+        return response()->json([
+            'status'   => $status,
+            'message'  => $message,
+            $urlKey    => $redirect ? admin_url($redirect) : '',
+        ]);
     }
 
     /**
@@ -85,6 +83,20 @@ trait HasFormResponse
         ]);
     }
 
+    /**
+     * 设置当前URL
+     *
+     * @param string $url
+     *
+     * @return $this
+     */
+    public function setCurrentUrl($url)
+    {
+        $this->currentUrl = admin_url($url);
+
+        return $this;
+    }
+
     /**
      * @param Request|null $request
      *
@@ -92,10 +104,14 @@ trait HasFormResponse
      */
     protected function getCurrentUrl(Request $request = null)
     {
+        if ($this->currentUrl) {
+            return $this->currentUrl;
+        }
+
         /* @var Request $request */
         $request = $request ?: (empty($this->request) ? request() : $this->request);
 
-        if ($current = $request->get('_current_')) {
+        if ($current = $request->get(static::CURRENT_URL_NAME)) {
             return url($current);
         }
 

+ 3 - 2
src/Widgets/Form.php

@@ -94,6 +94,7 @@ class Form implements Renderable
         }
 
     const REQUEST_NAME = '_form_';
+    const CURRENT_URL_NAME = '_current_';
 
     /**
      * @var string
@@ -744,7 +745,7 @@ JS
      */
     public function sanitize(array $input)
     {
-        Arr::forget($input, [static::REQUEST_NAME, '_token', '_current_']);
+        Arr::forget($input, [static::REQUEST_NAME, '_token', static::CURRENT_URL_NAME]);
 
         return $this->prepareInput($input);
     }
@@ -793,7 +794,7 @@ JS
             $this->method('POST');
             $this->action(route(admin_api_route('form')));
             $this->hidden(static::REQUEST_NAME)->default(get_called_class());
-            $this->hidden('_current_')->default($this->getCurrentUrl());
+            $this->hidden(static::CURRENT_URL_NAME)->default($this->getCurrentUrl());
         }
     }