How to Define Singleton Route in Laravel 9?

Laravel released 9.42 version recently with new features call singleton route. This feature contributed Jess Archer. Now in this tutorial, I will show you how to use and define the singleton routes in Laravel. By default, the singleton route works for get and put requests. But we can make it manually for almost all HTTP requests.

Let's see the example of how to define a singleton route in laravel 9 application:

routes/web.php

Route::singleton('user', UserController::class);

 

The singleton helper will automatically register the following routes for us:

routes/web.php

Route::get('/user', [UserController::class, 'show']);
Route::put('/user', [UserController::class, 'update']);
Route::get('/user/edit', [UserController::class, 'edit']);

 

Look at that, there is no method for create and destroy to create this route, define the singleton route like below:

routes/web.php

Route::singleton('user', UserController::class)->creatable();

 

Now it will generate routes for us like:

Route::get('/user', [UserController::class, 'show']);
Route::put('/user', [UserController::class, 'update']);
Route::get('/user/edit', [UserController::class, 'edit']);
Route::post('/user', [UserController::class, 'store']);
Route::get('/user/create', [UserController::class, 'create']);
Route::delete('/user', [UserController::class, 'destroy']);

 

Read also: What is Signed URL and How and When to Use It in Laravel

 

Hope it can help you.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js