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.
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");
}
@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 sign in or create an account to participate in this conversation.