Explain the concept of routing in Laravel.
Routing in Laravel is a fundamental feature that allows you to define the URLs your application should respond to and the actions that should be taken when those URLs are accessed. Here's a breakdown of how routing works in Laravel, including some examples:
Basics of Routing
In Laravel, routes are defined in the routes/web.php file for web routes or routes/api.php for API routes. Each route is associated with a URI (Uniform Resource Identifier) and a callback or controller action that should be executed when the route is accessed.
Basic Route
A basic route might look like this:
Route::get('/welcome', function () {
return 'Welcome to Laravel!';
});
In this example, when a user accesses /welcome, the application will return the string "Welcome to Laravel!".
Route with Parameters
Routes can also accept parameters. For example:
Route::get('/user/{id}', function ($id) {
return 'User ID is ' . $id;
});
In this case, if a user accesses /user/5, the response will be "User ID is 5".
Named Routes
You can name your routes to make them easier to reference:
Route::get('/profile', [UserProfileController::class, 'show'])->name('profile.show');
You can then generate URLs to this route using the route name:
$url = route('profile.show');
Route Groups
Route groups allow you to apply attributes to multiple routes. For example, applying middleware:
Route::middleware('auth')->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index']);
Route::get('/settings', [SettingsController::class, 'index']);
});
In this case, both /dashboard and /settings routes will use the auth middleware.
Resource Routes
For CRUD operations, Laravel provides a quick way to define resource routes:
Route::resource('posts', PostController::class);
This single line will create multiple routes for resourceful actions like:
GET /posts-indexmethodGET /posts/create-createmethodPOST /posts-storemethodGET /posts/{id}-showmethodGET /posts/{id}/edit-editmethodPUT/PATCH /posts/{id}-updatemethodDELETE /posts/{id}-destroymethod
Example Output
-
Basic Route:
URL:
http://your-app.test/welcome
Output:Welcome to Laravel! -
Route with Parameters:
URL:
http://your-app.test/user/10
Output:User ID is 10 -
Named Route:
URL:
http://your-app.test/profile(generated using route helper) -
Resource Route:
URL:
http://your-app.test/posts
Output: Uses theindexmethod ofPostController.
Routing in Laravel is a powerful tool that helps you organize and manage your application's URLs and their corresponding actions effectively.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
