How does routing work in Laravel?

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.


1. Where Routes Live in Laravel

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.


2. Defining a Basic Route

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


3. HTTP Methods in Laravel Routes

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'); // DELETE


You can also define routes for multiple methods at once:

Route::match(['get', 'post'], '/contact', 'ContactController@handle');
Route::any('/catch-all', 'CatchAllController@index');


4. Route Parameters

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";
});


5. Named Routes

Naming routes allows easy referencing.

Route::get('/profile', 'ProfileController@show')->name('profile.show');
// Usage
return redirect()->route('profile.show');


6. Route Groups

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');
});


7. Route Model Binding

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);


8. Middleware in Routes

Middleware filters requests before they reach the route’s logic.

Route::get('/dashboard', 'DashboardController@index')->middleware('auth');


9. Route Caching for Performance

In production, cache your routes for faster performance:

php artisan route:cache
Clear the cache when needed:
php artisan route:clear


10. Route Listing

You can see all registered routes:

php artisan route:list


Final Thoughts

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.

Comments