Jelajahi Sumber

修复多应用情况下路由别名冲突的问题;增加admin_route方法根据别名获取路由

jqh 4 tahun lalu
induk
melakukan
d8cec0958d

+ 1 - 1
src/Actions/HasActionHandler.php

@@ -70,7 +70,7 @@ trait HasActionHandler
      */
     public function handlerRoute()
     {
-        return route(admin_api_route('action'));
+        return route(admin_api_route_name('action'));
     }
 
     /**

+ 2 - 9
src/Admin.php

@@ -473,10 +473,8 @@ class Admin
     public static function routes()
     {
         $attributes = [
-            'domain'     => config('admin.route.domain'),
             'prefix'     => config('admin.route.prefix'),
             'middleware' => config('admin.route.middleware'),
-            'as'         => static::app()->getName().'.',
         ];
 
         if (config('admin.auth.enable', true)) {
@@ -511,18 +509,15 @@ class Admin
     /**
      * 注册api路由.
      *
-     * @param string $as
-     *
      * @return void
      */
-    public static function registerApiRoutes(string $as = null)
+    public static function registerApiRoutes()
     {
         $attributes = [
-            'domain'     => config('admin.route.domain'),
             'prefix'     => admin_base_path('dcat-api'),
             'middleware' => config('admin.route.middleware'),
-            'as'         => $as ?: static::app()->getApiRoutePrefix(Application::DEFAULT),
             'namespace'  => 'Dcat\Admin\Http\Controllers',
+            'as'         => 'dcat-api.',
         ];
 
         app('router')->group($attributes, function ($router) {
@@ -550,10 +545,8 @@ class Admin
         }
 
         $attributes = [
-            'domain'     => config('admin.route.domain'),
             'prefix'     => config('admin.route.prefix'),
             'middleware' => config('admin.route.middleware'),
-            'as'         => static::app()->getName().'.',
         ];
 
         app('router')->group($attributes, function ($router) {

+ 37 - 7
src/Application.php

@@ -132,9 +132,37 @@ class Application
      *
      * @return string
      */
-    public function getApiRoutePrefix(?string $app)
+    public function getApiRoutePrefix(?string $app = null)
     {
-        return "dcat.api.{$app}.";
+        return $this->getRoutePrefix($app).'dcat-api.';
+    }
+
+    /**
+     * 获取路由别名前缀.
+     *
+     * @param string|null $app
+     *
+     * @return string
+     */
+    public function getRoutePrefix(?string $app = null)
+    {
+        $app = $app ?: $this->getName();
+
+        return 'dcat.'.$app.'.';
+    }
+
+    /**
+     * 获取路由别名.
+     *
+     * @param string|null $route
+     * @param array $params
+     * @param bool $absolute
+     *
+     * @return string
+     */
+    public function getRoute(?string $route, array $params = [], $absolute = true)
+    {
+        return route("dcat.{$this->getName()}.$route", $params, $absolute);
     }
 
     /**
@@ -146,8 +174,8 @@ class Application
     {
         $this->switch($app);
 
-        $this->loadRoutesFrom(function () use ($app) {
-            Admin::registerApiRoutes($this->getApiRoutePrefix($app));
+        $this->loadRoutesFrom(function () {
+            Admin::registerApiRoutes();
         }, $app);
 
         if (is_file($routes = admin_path('routes.php'))) {
@@ -179,8 +207,10 @@ class Application
      */
     protected function loadRoutesFrom($path, ?string $app)
     {
-        if (! $this->container->routesAreCached()) {
-            Route::middleware('admin.app:'.$app)->group($path);
-        }
+        Route::group(array_filter([
+            'middleware' => 'admin.app:'.$app,
+            'domain'     => config("{$app}.route.domain"),
+            'as'         => $this->getRoutePrefix($app),
+        ]), $path);
     }
 }

+ 0 - 1
src/Console/stubs/routes.stub

@@ -7,7 +7,6 @@ use Dcat\Admin\Admin;
 Admin::routes();
 
 Route::group([
-    'domain'     => config('admin.route.domain'),
     'prefix'     => config('admin.route.prefix'),
     'namespace'  => config('admin.route.namespace'),
     'middleware' => config('admin.route.middleware'),

+ 1 - 1
src/Form/Field/Editor.php

@@ -126,7 +126,7 @@ class Editor extends Field
      */
     protected function defaultImageUploadUrl()
     {
-        return $this->formatUrl(route(admin_api_route('tinymce.upload')));
+        return $this->formatUrl(route(admin_api_route_name('tinymce.upload')));
     }
 
     /**

+ 1 - 1
src/Form/Field/Markdown.php

@@ -131,7 +131,7 @@ class Markdown extends Field
      */
     protected function defaultImageUploadUrl()
     {
-        return $this->formatUrl(route(admin_api_route('editor-md.upload')));
+        return $this->formatUrl(route(admin_api_route_name('editor-md.upload')));
     }
 
     /**

+ 1 - 1
src/Http/Middleware/Bootstrap.php

@@ -28,7 +28,7 @@ class Bootstrap
         if (
             config('admin.layout.dark_mode_switch')
             && ! Helper::isAjaxRequest()
-            && ! request()->routeIs(admin_api_route('*'))
+            && ! request()->routeIs(admin_api_route_name('*'))
         ) {
             Admin::navbar()->right((new DarkModeSwitcher())->render());
         }

+ 18 - 2
src/Support/helpers.php

@@ -437,14 +437,30 @@ if (! function_exists('admin_asset')) {
     }
 }
 
-if (! function_exists('admin_api_route')) {
+if (! function_exists('admin_route')) {
+    /**
+     * 获取路由别名.
+     *
+     * @param string|null $route
+     * @param array $params
+     * @param bool $absolute
+     *
+     * @return string
+     */
+    function admin_route(?string $route, array $params = [], $absolute = true)
+    {
+        return Dcat\Admin\Admin::app()->getRoute($route, $params, $absolute);
+    }
+}
+
+if (! function_exists('admin_api_route_name')) {
 
     /**
      * @param string $path
      *
      * @return string
      */
-    function admin_api_route(?string $path = '')
+    function admin_api_route_name(?string $path = '')
     {
         return Dcat\Admin\Admin::app()->getCurrentApiRoutePrefix().$path;
     }

+ 1 - 1
src/Traits/InteractsWithApi.php

@@ -83,7 +83,7 @@ trait InteractsWithApi
      */
     public function getRequestUrl()
     {
-        return $this->url ?: route(admin_api_route('value'));
+        return $this->url ?: route(admin_api_route_name('value'));
     }
 
     /**

+ 1 - 1
src/Traits/LazyWidget.php

@@ -19,7 +19,7 @@ trait LazyWidget
             'renderable' => $this->getRenderableName(),
         ]);
 
-        return route(admin_api_route('render'), $data);
+        return route(admin_api_route_name('render'), $data);
     }
 
     protected function getRenderableName()

+ 3 - 3
src/Widgets/Form.php

@@ -568,8 +568,8 @@ class Form implements Renderable
         if ($field instanceof Field\File && method_exists($this, 'form')) {
             $formData = [static::REQUEST_NAME => get_called_class()];
 
-            $field->url(route(admin_api_route('form.upload')));
-            $field->deleteUrl(route(admin_api_route('form.destroy-file'), $formData));
+            $field->url(route(admin_api_route_name('form.upload')));
+            $field->deleteUrl(route(admin_api_route_name('form.destroy-file'), $formData));
             $field->withFormData($formData);
         }
     }
@@ -831,7 +831,7 @@ HTML;
         if (method_exists($this, 'handle')) {
             $addHiddenFields = function () {
                 $this->method('POST');
-                $this->action(route(admin_api_route('form')));
+                $this->action(route(admin_api_route_name('form')));
                 $this->hidden(static::REQUEST_NAME)->default(get_called_class());
                 $this->hidden(static::CURRENT_URL_NAME)->default($this->getCurrentUrl());