Learn how to separate the Laravel route by file with this sample code snippet. Keeping all the routes of your Laravel application in one place is good enough for a small project but when it comes to a large project you will spend your whole day scrolling through the route file just to look for a specific thing. By this, you can separate the big route file into a lot of smaller route files that contain the routes for each module or each business domain instead.
In your web.php route
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
$folder = dirname(__FILE__) . "/web/*.php";
foreach (glob($folder) as $filename) {
include $filename;
}
Create web folder in the routes directory
just like this and in the routes/web/article.php | product.php you can write a route in that file.
In the routes/web/product.php
<?php
use App\Http\Controllers\ProductController;
use Illuminate\Support\Facades\Route;
Route::get('/product', [ProductController::class, 'index']);
In the routes/web/article.php
<?php
use App\Http\Controllers\ArticleController;
use Illuminate\Support\Facades\Route;
Route::get('/article', [ArticleController::class, 'index']);
php artisan route:list