In Laravel 12.x, routing is the foundation of how your application responds to incoming requests. Routes define the URLs users can access and connect them to the logic that should run. Laravel's routing system is simple, readable, and flexible. In this tutorial, you'll learn how to define routes, handle parameters, and organize your route definitions using Laravel's built-in tools.
What Is Routing in Laravel?
Routing is how Laravel matches an incoming request (like a user visiting /about
) to the code that should run in response. Laravel stores all route definitions in the routes/
directory. For web applications, the main file used is:
routes/web.php
Each route in this file maps a URL path to a closure or controller method.
Why Use Routing in Laravel?
Laravel's routing system gives you precise control over how your application handles web requests. It's designed to keep your codebase organized, maintainable, and secure. Key benefits include:
- Clean structure – All routes are defined in one place, making them easy to manage.
- Readable syntax – Routes are simple to write and understand.
- Secure handling – Only defined routes are accessible, reducing the risk of exposing unintended methods.
- Flexible logic – Easily route to closures, controllers, or views.
- Middleware support – Apply authentication, authorization, throttling, and other filters.
- URL parameters – Capture values from the URL and pass them to functions or controllers.
- Named routes – Reference routes without hardcoding URLs.
- Route grouping – Group similar routes with shared prefixes or middleware.
- Fallback routes – Handle invalid or undefined URLs gracefully.
Note: Laravel does not support auto-routing like CodeIgniter. All routes must be defined manually in the routes/
files. This improves security and gives you full control over which parts of your application are accessible.
Defining a Basic Route
You can define a simple route using the Route::get()
method. This method responds to HTTP GET requests.
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return 'Welcome to Laravel 12.x!';
});
This route responds to a request to /
(the homepage) and returns a plain message.
You can also return a view:
Route::get('/about', function () {
return view('about');
});
Importing the Route Facade
To use Laravel's routing functions like Route::get()
or Route::post()
, you need to import the Route
facade at the top of your route file:
use Illuminate\Support\Facades\Route;
The Route
facade gives access to Laravel's routing methods in a simple and readable way.
The Route
facade provides access to Laravel's routing methods. In PHP, the ::
symbol is called the scope resolution operator. It is used to call static methods or access constants in a class. Since Laravel facades act like static interfaces, you use Route::get()
to define routes without creating an object.
Common Route Methods
Laravel supports all common HTTP verbs:
Method | Description |
---|---|
Route::get()
| Handles GET requests |
Route::post()
| Handles POST requests |
Route::put()
| Handles PUT requests |
Route::patch()
| Handles PATCH requests |
Route::delete()
| Handles DELETE requests |
Route::match()
| Handles multiple methods |
Route::any()
| Handles all HTTP methods |
Example using post
:
Route::post('/submit', function () {
return 'Form submitted!';
});
Route Parameters
Laravel allows you to capture values from the URL and use them in your application. These values are called route parameters.
Required Parameters
Use curly braces {}
to define a required parameter in the route. Laravel automatically passes the value to the route's function.
Route::get('/product/{category}', function ($category) {
return "Product category name: $category";
});
If a user visits /product/shoes
, the value shoes
is passed to the route, and it will return:
Product category name: shoes
Optional Parameters
Add a ?
after the parameter name to make it optional. You should also provide a default value.
Route::get('/post/{slug?}', function ($slug = 'default-post') {
return "Post: $slug";
});
Visiting /post/hello-world
returns Post: hello-world
, and /post
returns Post: default-post
.
Named Routes
Named routes allow you to reference routes by name instead of the URL. This is useful for generating links and redirects.
Route::get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
You can use this name in redirects:
return redirect()->route('dashboard');
Grouping Routes
You can group routes to apply shared settings like URL prefixes or middleware. This helps keep your code clean and organized.
Using Prefix
The example below adds the store
prefix to all routes inside the group:
Route::prefix('store')->group(function () {
Route::get('/products', function () {
return 'Store Products';
});
Route::get('/offers', function () {
return 'Store Offers';
});
});
This will register the following URLs:
/store/products
/store/offers
These routes now share the same /store
path segment, making route management more efficient.
Route Fallback
A fallback route is used when no other defined route matches the incoming request. This is helpful for showing a custom "Page Not Found" message instead of the default 404 error.
Route::fallback(function () {
return 'Page not found.';
});
This route will run if the user visits a URL that doesn't match any other route.
You can also return a custom view:
Route::fallback(function () {
return view('errors.404');
});
Use fallback routes to handle unknown URLs gracefully.
Routes and Controllers
Instead of writing logic inside the route file, you can point routes to controller methods for better organization.
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'index']);
This route calls the index()
method in HomeController
.
Route Files Overview
Laravel organizes routes into different files based on their use:
File | Purpose |
---|---|
web.php
| Web routes with sessions, CSRF |
api.php
| Stateless API routes |
console.php
| Artisan command routes |
channels.php
| Broadcast channel definitions |
Conclusion
In this tutorial, you learned how routing works in Laravel 12.x. You defined simple routes, used HTTP methods, handled route parameters, applied route names, grouped related routes, created fallback routes, and connected routes to controllers. Routing is essential for controlling how users interact with your application. With this knowledge, you're ready to start building pages and handling user input using Laravel routes.