Basic Routing in Laravel
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
This article summarizes the basic way to write routes in Laravel.
For versions before Laravel 7, please refer to this article.
→ How to write routing in Laravel 6. A selection of essential patterns to remember
Subsequent versions are the same as described in this article.
Where to Write Routing
You write it in files like:
/route/web.php
/route/api.php
Basic Way to Write Routing
Writing Everything in One Place (Not Recommended)
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');
This approach can make the routing hard to read as the number of routes increases or when the API processing becomes complex.
Instead, routes should be written to pass the request to a controller for processing.
Minimal Writing Style
use App\Http\Controllers\UserController;
Route::get('/user', [UserController::class, 'index']);
How to Name Routes
Route::get('/posts', [PostController::class, 'index'])->name('post.index'); // Adding a name
By assigning a name, you can use it with the route() helper function.
route('post.index')
Example of Usage:
<a href={{ route('post.index') }}>Post List</a>
return redirect()->route('post.index');
The benefit of naming is that if the URL changes, you only need to modify the routing.
Adding Parameters
Route::get('/user/{id}', [UserController::class, 'show'])->name('user.show');
By enclosing {id}
in curly braces, the route is set up to accept a parameter.
You can also specify conditions for the parameters.
The following example allows only numeric IDs.
Route::get('/user/{id}', [UserController::class, 'show'])->name('user.show')->where('id', '[0-9]+');
Using Parameters in the Controller
While you can use any variable as an argument, it's safer to write what you've defined in the routing.
public function show($id) // Write it as an argument.
{
dd($id); // You can use the parameter here
$user = User::find($id);
}
Writing Links in Blade Files
<a href="{{ route('user.show', ['id' => 1]) }}">View Profile</a>
Implicit Binding
In short, there is a mechanism that automatically fetches the model instance by matching the parameter ID with the Model's ID.
Any parameter written in the routing will work, but it is advisable to keep it related to the model name.
Route::get('/user/{user}', [UserController::class, 'show'])->name('user.show')->where('user', '[0-9]+');
public function show(User $user) // Retrieves the User model instance matching the ID parameter
{
dd($user); // If accessed via `/user/1`, it retrieves information of the user with ID 1.
}
Knowing this is helpful during authorization.
Grouping Routes
Sometimes, routes can be repetitive when they perform similar functions.
For example, routes accessible only to authenticated users or those only accessible to unauthenticated users.
// Only authenticated users
Route::middleware('auth:sanctum')->group(function () {
Route::get('user/{id}', [UserController::class,'show']);
});
This group can be organized by:
- Middleware
- Controller
- Subdomain Routing
- Route Prefixes
- Route Name Prefixes
All five can be used in combination.
Route::middleware('auth:sanctum')->group(function () {
Route::prefix('user')->group(function () {
Route::get('/{id}', [UserController::class,'show']);
});
});
Benefits
By carefully deciding what to group, routing becomes significantly easier to read.
Route::middleware('auth:sanctum')->group(function () {
Route::prefix('post')->group(function () {
Route::get('/', [PostController::class,'index']);
Route::get('/{id}', [PostController::class,'show']);
Route::post('/', [PostController::class,'store']);
Route::patch('/{id}', [PostController::class,'update']);
Route::delete('/{id}', [PostController::class,'destroy']);
});
});
Although the processing is identical, without grouping it would look like this:
Route::prefix('post')->get('/', [PostController::class,'index'])->middleware('auth:sanctum');
Route::prefix('post')->get('/{id}', [PostController::class,'show'])->middleware('auth:sanctum');
Route::prefix('post')->post('/', [PostController::class,'store'])->middleware('auth:sanctum');
Route::prefix('post')->patch('/{id}', [PostController::class,'update'])->middleware('auth:sanctum');
Route::prefix('post')->delete('/{id}', [PostController::class,'destroy'])->middleware('auth:sanctum');
While grouping can make it easier to read, there are cases where it can complicate things, so it's essential to establish a clear strategy.
Conclusion
There are various ways to write routing, but I've only highlighted the commonly used methods.
If you want to know everything, please check the following site.