Getting Started with Routes in Laravel

Routes
Routes

Now that we have Laravel 12 installed on our local machine, let’s jump into something practical. While we will eventually cover important concepts such as configuration settings, the folder structure, and useful Artisan commands, I don’t want this to be a boring, theory-only journey. Instead, we’ll learn these topics step-by-step while working on real examples.

Our first practical task will be to create web page URLs in Laravel. In Laravel, these URLs are defined using something called routes. Routes act like traffic signs for your application — they tell Laravel which page or functionality to load when someone visits a specific URL.

Let’s get started and see how routes work in action!


In the root directory of a Laravel project, you will find a folder named routes. Open this folder, and you will see a file called web.php. This file is where we define all the web routes for our application. If you open it, you will notice that some routes are already defined. For example, you might see the following code:

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
return view('welcome');
});


Let’s break this down:

  • The first line, use Illuminate\Support\Facades\Route;, imports the Route service. This service is required to define routes in Laravel. Without this line, the code below it will throw an error.
  • The Route::get('/', function () { return view('welcome'); }); line defines a route for the home URL (/). When a user visits the home page, Laravel will return the welcome view.

This is the basic structure of a route in Laravel, and it allows you to map URLs to specific actions in your application.

When we access this route in a browser, the following screen will appear because the route is loading the welcome.blade.php view. The image below shows this screen. The welcome.blade.php file is located in the resources/views directory.

This image has an empty alt attribute; its file name is WhatsApp-Image-2025-08-12-at-6.48.12-PM.jpeg

Basic Routing

In Laravel, routes are defined in the routes/web.php file. Each route associates a URL pattern with a specific action, such as returning a view or executing a function.

Defining a Basic Route

To define a route that responds to a GET request at the /greeting

use Illuminate\Support\Facades\Route;

Route::get('/greeting', function () {
return 'Hello World';
});


This route will return the string “Hello World” when accessed via a browser or HTTP client.

This image has an empty alt attribute; its file name is route_greeting.jpeg

Route Files

All routes are defined in the routes directory

  • routes/web.php: For web routes that require session state, CSRF protection, and cookie encryption.
  • routes/api.php: For stateless API routes that are typically used for APIs and do not maintain session state.
  • routes/console.php: For defining console-based routes (Artisan commands).
  • routes/channels.php: For registering event broadcasting channels.

These files are automatically loaded by Laravel using the configuration specified in your application’s bootstrap/app.php file.


Route Parameters

You can capture segments of the URI as parameters:

Route::get('/user/{id}', function ($id) {
return "User ID: {$id}";
});


This route will capture the id parameter from the URL and pass it to the closure.


Route Middleware

Middleware provides a convenient mechanism for filtering HTTP requests entering your application. You can assign middleware to routes to perform tasks like authentication, logging, or input validation.

Assigning Middleware to a Route

To assign middleware to a route

use App\Http\Middleware\EnsureTokenIsValid;

Route::get('/profile', function () {
// ...
})->middleware(EnsureTokenIsValid::class);


This ensures that the EnsureTokenIsValid middleware is executed before the route’s action.


Named Routes

Named routes allow you to generate URLs or redirects for specific routes by their name:

Route::get('/profile', function () {
// ...
})->name('profile');


You can then generate a URL to this route:

$url = route('profile');


This is particularly useful for generating URLs in views or redirects.


Route Groups

Route groups allow you to share attributes, such as middleware or namespaces, across a large number of routes without needing to define those attributes on each individual route.

Defining a Route Group

To define a group of routes that share the same middleware:

Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', function () {
// Only authenticated users may access this route...
});

Route::get('/account', function () {
// Only authenticated users may access this route...
});
});


This ensures that the auth middleware is applied to both routes within the group.


By understanding and utilizing these basic routing features, you can effectively define and manage the routes in your Laravel applications.