2 Laravel 11 Basics: Routing

AI Bot
By AI Bot ·

Loading the Text to Speech Audio Player...

Introduction

Routing is a fundamental aspect of Laravel, enabling developers to define URL paths and associate them with specific actions in their applications. This article covers the basics of routing in Laravel 11, including defining routes, using route parameters, middleware, and more.

Basic Routing

The simplest Laravel routes accept a URI and a closure, providing a straightforward way to define routes and their behavior without complicated configuration files:

use Illuminate\Support\Facades\Route;
 
Route::get('/greeting', function () {
    return 'Hello World';
});

The Default Route Files

All Laravel routes are defined in your route files, located in the routes directory. These files are automatically loaded by Laravel based on the configuration in your bootstrap/app.php file. The routes/web.php file defines routes for your web interface, which are assigned the web middleware group providing features like session state and CSRF protection.

use App\Http\Controllers\UserController;
 
Route::get('/user', [UserController::class, 'index']);

Route Parameters

Laravel routes can accept parameters, making them dynamic. For example, you can capture a user's ID from the URL:

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

Optional Parameters

Sometimes, you may need to specify a route parameter that may not always be present in the URI. Use a ? mark after the parameter name and provide a default value:

Route::get('/user/{name?}', function ($name = 'John') {
    return $name;
});

Regular Expression Constraints

You can constrain the format of your route parameters using the where method:

Route::get('/user/{id}', function ($id) {
    // ...
})->where('id', '[0-9]+');

Named Routes

Named routes provide a convenient way to generate URLs or redirects for specific routes. Assign a name to a route using the name method:

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

Generate URLs or redirects using the route's name:

$url = route('profile');
return redirect()->route('profile');

Middleware

Middleware allows you to filter HTTP requests entering your application. Create middleware using the Artisan command:

php artisan make:middleware CheckAge

Define middleware logic in app/Http/Middleware/CheckAge.php:

public function handle($request, Closure $next)
{
    if ($request->age <= 200) {
        return redirect('home');
    }
 
    return $next($request);
}

Assign middleware to routes in routes/web.php:

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

Route Groups

Route groups allow you to share route attributes across a large number of routes without needing to define those attributes on each individual route:

Route::middleware(['first', 'second'])->group(function () {
    Route::get('/', function () {
        // Uses first & second middleware...
    });
    Route::get('/user/profile', function () {
        // Uses first & second middleware...
    });
});

Conclusion

Understanding the basics of routing in Laravel 11 is crucial for building robust applications. This guide covered the fundamental aspects, including defining routes, using parameters, and middleware. For more information, refer to the official Laravel documentation.

Explore more about Laravel 11's powerful routing features in the official documentation.

FeatureDescription
Basic RoutingDefine simple routes with a URI and closure
Route ParametersCapture dynamic values from the URI
MiddlewareFilter HTTP requests entering your application

References


Want to read more tutorials? Check out our latest tutorial on Creating Your First Airtable Extension: A Step-by-Step Guide to Custom Functionality.

Discuss Your Project with Us

We're here to help with your web development needs. Schedule a call to discuss your project and how we can assist you.

Let's find the best solutions for your needs.