Laravel 11 RouteServiceProvider Configuration Example
laravel 11 removed RouteServiceProvider.php file, they give an options to configure about routes in app.php.
So, you will have a question if i want to create a custom route file then how to define without RouteServiceProvider. If lower then laravel 11 version than you can do it with RouteServiceProvider.php file. But laravel 11 provides options in app.php file to define custom routes file options and you can customize routes from that file as well.
Before Laravel 11 you are defining custom route file like the below code:
app/Providers/RouteServiceProvider.php
public function boot()
{
$this->routes(function () {
Route::middleware('web')
->prefix('admin')
->group(base_path('routes/admin.php'));
});
}
Now, In Laravel 11 Version, You can define custom route file like the following way:
bootstrap/app.php
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Support\Facades\Route;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
channels: __DIR__.'/../routes/channels.php',
health: '/up',
then: function () {
Route::middleware('web')
->prefix('admin')
->group(base_path('routes/admin.php'));
}
)
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
Next, you need to create custom admin.php file like the following way:
routes/admin.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/dashboard', [App\Http\Controllers\HomeController::class, 'index']);
Route::get('/users', [App\Http\Controllers\UserController::class, 'index']);
Route::get('/posts', [App\Http\Controllers\PostController::class, 'index']);
Now, you can run the following command to see the list of routes:
php artisan route:list
You will see the following routes:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 11 Apexcharts using Larapex Charts Example
- Laravel 11 Socialite Login with Slack Account Example
- Laravel 11 Real-Time Notifications using Pusher Example
- Laravel 11 Socialite Login with Facebook Account Example
- Laravel 11 Socialite Login with Gitlab Account Example
- Laravel 11 Socialite Login with Github Account Example
- How to Integrate ChatGPT API with Laravel 11?
- Laravel 11 Comment System with Replies Example
- Laravel 11 Notifications With database Driver Example
- Laravel 11 Send Email Via Notification Example
- Laravel 11 - Install and Configure Laravel Debugbar
- Laravel 11 Like Dislike System Tutorial Example
- Laravel 11 Pagination with Relationship Example