在多个文件中拆分 Laravel / Lumen 路由

Splitting Laravel / Lumen routes in multiple files

Lumen 框架附带 routes/web.php 文件。阅读有关如何在多个文件中拆分路由的信息时,我看到了 Laravel 文档(不是 Lumen),那里看起来很清楚。

@see https://laravel.com/docs/6.x/routing#basic-routing >>> 默认路由文件

上面写着

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface. ...

Routes defined in the routes/api.php file are nested within a route group by the RouteServiceProvider. Within this group, the /api URI prefix is automatically applied so you do not need to manually apply it to every route in the file. You may modify the prefix and other route group options by modifying your RouteServiceProvider class

所以你可以添加其他路由文件并编辑 app/Providers/RouteServiceProvider.php class, 这看起来非常直接和清晰。

只是流明没有app/Providers/RouteServiceProvider.phpclass

那么在不破坏框架的情况下定义自己的路由文件的最佳方法是什么?

谢谢!

Lumen 中的等价物是 located in /bootstrap/app.php

您可以在那里适当地添加路由文件条目。如您所见,实际上并没有用于添加文件或任何内容的特定 API。所以只写你认为合适的逻辑。

我们可以像 Laravel 那样做。

在根文件夹中创建 routes 目录。

在路由目录中创建文件,例如, 像 routes/users.phproutes/posts.php

添加以上路由文件,在bootstrap/app.php文件

// Load The Application Routes
$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__.'/../routes/web.php';
    require __DIR__.'/../routes/users.php'; // mention file names 
    require __DIR__.'/../routes/posts.php';
});

如果您的路线位于另一个文件夹中,例如 app/Api/V1,而您的控制器位于 app/Api/V1/Controllers 中,那么您可以在 bootstrap/app.php

中使用以下代码

文件夹结构 对于路线:app->Api->V1->routes.php 对于控制器:app->Api->V1->Controllers

代码:

$app->router->group([
    'namespace' => 'App\Api\V1\Controllers',
], function ($router) {
    require __DIR__.'/../app/Api/V1/routes.php';
});