Table of Contents
Introduction
Laravel is a PHP web application framework that provides a simple and elegant way to organize and handle routes in a web application. In this tutorial, we will cover the basics of routing in Laravel and dive into some of the more advanced features that it offers.
What is Routing?
First, let’s start with the basics. In Laravel, routes are defined in the routes
directory within the app
directory. By default, there are two files in this directory: web.php
and api.php
. The web.php
file is used for defining routes for web pages, while the api.php
file is used for defining routes for API endpoints.
To define a basic route, you can use the Route
facade provided by Laravel. The Route
facade has several methods for defining different types of routes, such as get
, post
, put
, and delete
. Here is an example of a basic route definition:
Route::get('/', function () {
return 'Hello World!';
});
This route will match any GET request to the root of the application and return the string “Hello World!”.
Examples of Laravel Routing:
Another way to define routes is by using controllers. You can create a controller by running the command php artisan make:controller <controller-name>
. Then you can define a route that will call a specific method of the controller.
Route::get('/', [HomeController::class, 'index']);
This route will match any GET request to the root of the application and call the index
method of the HomeController
class.
Laravel also provides a way to define routes with parameters. For example, you can define a route that will match a specific user by their ID:
Route::get('/users/{id}', function ($id) {
return 'User '.$id;
});
This route will match any GET request to /users/{id}
and pass the value of id
as a parameter to the closure or the controller method.
Laravel also provides a way to define routes with optional parameters and constrains on parameter.
Route::get('users/{name?}', function ($name = null) {
return $name;
});
Route::get('users/{name}', function ($name) {
//
})->where('name', '[A-Za-z]+');
This route will match any GET request to /users/
and also accepts a name parameter which is optional. and if name is passed it should match regex [A-Za-z]+
Laravel also provides a way to group routes together and apply middleware to them.
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', function () {
//
});
Route::get('/profile', function () {
//
});
});
This group of routes will only be accessible to authenticated users.
Lastly, Laravel also provides a way to define fallback routes that will be executed if no other routes match the current request.
Route::fallback(function () {
return 'Sorry, the page you are looking for could not be found.';
});
This fallback route will be executed if no other routes match the current request.
In this tutorial, we covered the basics of routing in Laravel, including how to define basic routes, how to use controllers to handle routes, how to define routes with parameters, optional parameters and constraints, how to group routes together and apply middleware to them, and how to define fallback routes.
It’s worth noting that Laravel also provides a way to name routes, which can be useful for generating URLs or redirects. You can name a route by passing a second argument to the Route
facade’s methods.
Route::get('/users/{id}', function ($id) {
//
})->name('users.show');
You can also use the route
function to generate URLs for named routes.
$url = route('users.show', ['id' => 1]);
It is also worth noting that Laravel provides a way to namespace routes, which can be useful for organizing related routes together. For example, you can define a namespace for all routes related to admin functionality:
Route::namespace('Admin')->group(function () {
Route::get('/admin', [AdminController::class, 'index']);
Route::get('/admin/users', [AdminController::class, 'users']);
});
In this tutorial, we covered the basics of routing in Laravel, including how to define basic routes, how to use controllers to handle routes, how to define routes with parameters, optional parameters and constraints, how to group routes together and apply middleware to them, how to define fallback routes, how to name routes and how to namespace routes. With this knowledge, you should have a good understanding of how to organize and handle routes in a Laravel web application.