3
votes

Lumen framework comes with the routes/web.php file. Reading about how to split the routes in multiple files I came across Laravel documentation (not Lumen) and there it seems pretty clear.

@see https://laravel.com/docs/6.x/routing#basic-routing >>> The Default Route Files

It states

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

So you can just add other route files and edit the app/Providers/RouteServiceProvider.php class, this seems pretty straight and clear.

Just that Lumen doesn't have any app/Providers/RouteServiceProvider.php class

So then what's the best way to define your own route files without mangling the framework?

Thanks!

2
Simply include files in web.php? Or is that too simple ;) I don't have Lumen experience, only a lot of Laravel experience.Rolf

2 Answers

6
votes

We can do this just like Laravel.

Make routes directory in root folder.

Inside routes directory create files, for instance, like routes/users.php, routes/posts.php

Add above route files, in bootstrap/app.php file

// 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';
});
5
votes

The equivalent in Lumen is located in /bootstrap/app.php.

You can add routes file entries there appropriately. As you can see, there isn't really a specific API for adding files or anything. So just write the logic as you see fit.