This in-depth guide covers everything you need to know about Laravel routing. We’ll explore how routes work, how to define them, pass parameters, use controllers, and group routes with middleware. By the end, you’ll know how to build clean, flexible, and maintainable URLs in Laravel applications. Perfect for beginners and intermediate developers who want a professional step-by-step tutorial.
When building web applications, one of the most important aspects is defining how users access different parts of your app. In Laravel, this is handled through routing. Routing maps user requests (like visiting /home) to the appropriate response, such as returning a view, running a controller method, or sending JSON data.
Laravel offers one of the cleanest and most powerful routing systems in the PHP world. It is flexible, expressive, and developer-friendly, allowing you to easily define routes for web pages, APIs, and even command-line applications.
What is Routing in Laravel?
Routing is the process of defining how Laravel should respond to different HTTP requests. For example, when a user types https://yourapp.com/about in their browser, Laravel decides what should happen based on the rules you define in your route files.
Laravel routes are defined inside the routes/ directory. The most common file is routes/web.php for web routes, and routes/api.php for API routes.
Basic Routing
Defining a Simple Route
A basic Laravel route looks like this:
// routes/web.php
use Illuminate\Support\Facades\Route;
Route::get('/hello', function () {
return "Hello, Laravel!";
});
When you visit http://localhost:8000/hello, Laravel will return the string "Hello, Laravel!".
Different HTTP Verbs
Laravel allows routes for different HTTP methods like GET, POST, PUT, PATCH, and DELETE.
// GET request
Route::get('/users', function () {
return 'List of users';
});
// POST request
Route::post('/users', function () {
return 'User created';
});
// PUT request
Route::put('/users/{id}', function ($id) {
return "Update user {$id}";
});
// DELETE request
Route::delete('/users/{id}', function ($id) {
return "Delete user {$id}";
});
Route Parameters
Laravel routes often require parameters to pass dynamic values in the URL. For example, showing a user profile with an ID.
Grouping routes allows you to apply common settings like middleware, prefixes, or namespaces to multiple routes.
Using Prefix
Route::prefix('admin')->group(function () {
Route::get('/dashboard', function () {
return 'Admin Dashboard';
});
Route::get('/users', function () {
return 'Manage Users';
});
});
This creates routes /admin/dashboard and /admin/users.
Using Middleware
Route::middleware(['auth'])->group(function () {
Route::get('/profile', function () {
return 'User Profile';
});
Route::get('/settings', function () {
return 'Account Settings';
});
});
Routes with Controllers
Instead of writing logic inside closures, Laravel encourages using controllers for cleaner code.
// routes/web.php
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);
Controller Example:
// app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index() {
return "Showing all users";
}
}
Route Caching for Performance
In production, you can cache your routes to make your application faster.
php artisan route:cache
php artisan route:clear
This significantly reduces route registration time on large applications.
Comparison of Route Features
Here’s a quick comparison table for common routing features:
Feature
Example
Usage
Basic Route
Route::get('/home')
Define static pages
Route Parameter
Route::get('/user/{id}')
Dynamic values in URL
Optional Parameter
{name?}
Default values
Named Route
->name('route')
Easy referencing
Route Group
Route::prefix('admin')
Shared prefix or middleware
Controller Route
[Controller::class, 'method']
Better structure
Conclusion
Routing in Laravel is both simple and powerful. You can start with basic routes and gradually add complexity using parameters, groups, middleware, and controllers. Mastering routing will allow you to create clean, SEO-friendly, and scalable URLs that enhance both developer productivity and user experience.
By following this tutorial, you should now have a strong foundation in Laravel routing and be ready to build modern, maintainable applications with confidence.
This article is a comprehensive tutorial on using the Prohibited Validation Rules in Laravel. You will learn how to apply prohibited, prohibited_if, and prohibited_unless with clear explanations, real-life scenarios, and code examples. This guide is perfect for developers who want to master advanced validation techniques in Laravel applications.
This detailed tutorial explores request validation in Laravel controllers. You’ll learn multiple techniques—basic controller validation, using form request classes, custom rules, conditional validation, error handling, localization, and best practices. With practical examples, code snippets, and structured explanations, this article is designed for beginners to advance learner.
This guide teaches you how to deploy Laravel applications to production servers. From preparing your environment and configuring Nginx or Apache, to database migrations, caching, performance optimization, CI/CD pipelines, and security practices—this article covers everything step by step.It’s suitable for both beginners and advanced developers who want to ship stable, secure & scalable app.
This website uses cookies to enhance your browsing experience. By continuing to use this site, you consent to the use of cookies. Please review our Privacy Policy for more information on how we handle your data. Cookie Policy
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.