Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JenuelDev-31676163's avatar

Best Way to Organize Routes Specially the API route

So currently our api.php has a ton of lines of code in it. More than 1000 lines code.

This is because we have a lot of API. But what I want is to organize it that way, I can move api groups to a separate file.

This is the solution I am planning but, according to some people this has performance issues

Route::prefix('customer')->group(base_path('/path/to/file.php'));

The alternative I get was doing it like this, using a require:

Route::prefix('customer')->group(function () {
    require base_path('/path/to/file.php');
});

But does this good thought? what do you guys think? and what would be the best way to orginize. Thanks thanks.

0 likes
3 replies
Tray2's avatar
Tray2
Best Answer
Level 73

One way to go about it is to create a small helper function.

The you could do something like this.

<?php
  routesForModel('books');
  routesForModel('games');
  routesForModel('movies');
  routesForModel('records');
  routesForModel('profiles');

In the Models/books.php

Route::get('/books', BooksIndexController::class)->name('books.index');
Route::get('/books/create', BooksCreateController::class)->name('books.create');
Route::get('/books/{bookShowView}', BooksShowController::class)->name('books.show');
Route::post('/books/store', BooksStoreController::class)->name('books.store');
Route::get('/books/edit/{bookShowView}', BooksEditController::class)->name('books.edit');
Route::put('/books/{book}', BooksUpdateController::class)->name('books.update');
Route::delete('/books/{book}', BooksDeleteController::class)->name('books.delete');

And the helper function

function routesForModel($model)
{
    return require base_path("routes/Models/{$model}.php");
}
1 like
JenuelDev-31676163's avatar

@Tray2 Oh yeah.. thanks for this one, I think this is a good way to implement it. Can I know why we need to use a require instead of doing it like this?:

Route::prefix('customer')->group(base_path('/path/to/file.php'));

thanks thanks

Please or to participate in this conversation.