jqh 5 years ago
parent
commit
61bb437d57
2 changed files with 34 additions and 10 deletions
  1. 23 1
      src/Application.php
  2. 11 9
      src/Extension.php

+ 23 - 1
src/Application.php

@@ -85,6 +85,28 @@ class Application
         }
     }
 
+    /**
+     * 注册路由.
+     *
+     * @param string|\Closure $pathOrCallback
+     */
+    public function routes($pathOrCallback)
+    {
+        $this->loadRoutesFrom($pathOrCallback, static::DEFAULT);
+
+        if ($this->apps) {
+            foreach ($this->apps as $app => $enable) {
+                if ($enable) {
+                    $this->withConfig($app);
+
+                    $this->loadRoutesFrom($pathOrCallback, $app);
+                }
+            }
+
+            $this->withConfig(static::DEFAULT);
+        }
+    }
+
     protected function registerMultiAppRoutes()
     {
         foreach ($this->apps as $app => $enable) {
@@ -150,7 +172,7 @@ class Application
      *
      * @return void
      */
-    protected function loadRoutesFrom(string $path, ?string $app)
+    protected function loadRoutesFrom($path, ?string $app)
     {
         if (! $this->container->routesAreCached()) {
             Route::middleware('admin.app:'.$app)->group($path);

+ 11 - 9
src/Extension.php

@@ -363,15 +363,17 @@ abstract class Extension
      */
     public function routes($callback)
     {
-        $attributes = array_merge(
-            [
-                'prefix'     => config('admin.route.prefix'),
-                'middleware' => config('admin.route.middleware'),
-            ],
-            $this->config('route', [])
-        );
-
-        Route::group($attributes, $callback);
+        Admin::app()->routes(function ($router) use ($callback) {
+            $attributes = array_merge(
+                [
+                    'prefix'     => config('admin.route.prefix'),
+                    'middleware' => config('admin.route.middleware'),
+                ],
+                $this->config('route', [])
+            );
+
+            $router->group($attributes, $callback);
+        });
     }
 
     /**