Routing is one of the most essential features of Laravel. It determines how your application responds to user requests via URLs. If you’ve ever typed a URL into your browser and received a page in return, you’ve already interacted with routing. In Laravel, routes define which controller method or closure handles a specific request.
In this article, we’ll cover everything from basic routes to advanced features like route model binding and middleware.
By default, Laravel stores its route definitions in the routes directory:
routes/web.php → Routes for web pages (HTML responses).
routes/api.php → Routes for API endpoints (JSON responses).
routes/console.php → Artisan command routes.
routes/channels.php → Broadcast channel routes for real-time events.
The simplest way to define a route is using the Route facade.
// routes/web.php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return 'Hello, Laravel!';
});
Here:
get → HTTP method
'/' → URI
Closure → Code that runs when the route is visited
Laravel supports multiple HTTP methods:
Route::get('/home', 'HomeController@index'); // GET
Route::post('/form', 'FormController@store'); // POST
Route::put('/update/{id}', 'UserController@update'); // PUT
Route::delete('/delete/{id}', 'UserController@destroy'); // DELETEYou can also define routes for multiple methods at once:
Route::match(['get', 'post'], '/contact', 'ContactController@handle');
Route::any('/catch-all', 'CatchAllController@index');
Routes can accept dynamic segments.
Required Parameters
Route::get('/user/{id}', function ($id) {
return "User ID: $id";
});Optional Parameters
Route::get('/user/{name?}', function ($name = 'Guest') {
return "Hello, $name";
});Naming routes allows easy referencing.
Route::get('/profile', 'ProfileController@show')->name('profile.show');
// Usage
return redirect()->route('profile.show');You can group routes to share settings like middleware, prefixes, or namespaces.
Route::prefix('admin')->middleware('auth')->group(function () {
Route::get('/dashboard', 'AdminController@dashboard');
Route::get('/users', 'AdminController@users');
});Laravel can automatically inject model instances into routes.
Implicit Binding
Route::get('/post/{post}', function (App\Models\Post $post) {
return $post;
});Explicit Binding
Route::model('user', App\Models\User::class);Middleware filters requests before they reach the route’s logic.
Route::get('/dashboard', 'DashboardController@index')->middleware('auth');In production, cache your routes for faster performance:
php artisan route:cache
Clear the cache when needed:
php artisan route:clear
You can see all registered routes:
php artisan route:list
Laravel’s routing system is powerful, expressive, and beginner-friendly. Whether you’re building simple pages or a complex API, mastering routing will help you structure your application effectively.
By understanding the fundamentals—basic routes, parameters, groups, middleware, and model binding—you’ll be ready to handle any routing scenario in Laravel.