Routing — Laravel 10 Discovery Training
In this new chapter where we will see together the operation of Routing which will allow Laravel to match a particular URL with a specific piece of code. To use the Routing system, we will go to the folder routes
and we will modify the file web.php
. Inside this file, we see that there is already a route defined.
use Illuminate\Support\Facades\Route;
Route::get("https://grafikart.fr/", function () {
return view('welcome');
});
Routes are defined using the class Illuminate\Support\Facades\Route
. To define a new path, we will use a method that has the same name as the HTTP method. Then, we will put in first parameter the URL, and in second parameter a function which will make it possible to explain how to answer.
So there, for example, this route translates as follows: “when I access the root page, you must respond by returning a view called welcome
. We’ll talk about views a little later, but views are simply PHP files that allow you to generate HTML rendering. So if I look in the “resources” folder, we will have in the “view” folder a “welcome.blade.php” file which will simply contain the HTML code that we see on the screen when I go to the home page.
This Routing system will allow us to declare new routes. In my case, I’d like to respond to the “/blog” route. For this, we will use the same class, we will also respond to the method get
, we will put our URL “/blog” and then we will define a function. Since we haven’t seen the views yet, we’ll just return a string that says “hello”.
use Illuminate\Support\Facades\Route;
Route::get("https://grafikart.fr/blog", function () {
return view('welcome');
});
If you return a string, Laravel will automatically return the correct ones at the top and this code will be displayed in the browser as an HTML page.
What is interesting is that if we ever decide to return something that cannot be converted into a character string, for example an array, in this case, it will return a header of the type “JSON “, and it will convert our PHP array to “json” which it will then send back to the browser.
use Illuminate\Support\Facades\Route;
Route::get("https://grafikart.fr/blog", function () {
return [
'title' => 'Mon premier article',
'content' => 'Ceci est le contenu de mon article'
];
});
By default, it’s smart enough to know what type of response it should give based on the return type of our function.
Request object
Now, what happens if we want to manage parameters in the url? For example ?name=john
. By default, what we would do in a standard PHP application is we would put a call to $_GET
and we would go get the key name
. This is not the correct way to do this within a Laravel application. With the framework, when I use a function, I can inject it with an additional parameter that will allow to retrieve information about the request. This parameter must be of type Illuminate\Http\Request
.
We will find a lot of useful methods on this object Request
. To recover the parameters we have 2 methods which will be interesting.
all()
allows to retrieve all the parameters in the form of an arrayinput($key)
allows to retrieve a parameter specifically and will return null if it does not exist
use Illuminate\Http\Request;
Route::get("https://grafikart.fr/blog", function (Request $request) {
$request->input('name')
});
We will have the opportunity to come back to this object Request
and its methods later.
Dynamic URLs
Now, what we would like to do is to have URLs that are closer to reality. For example, if I go to a blog, the article urls often contain a slug followed by an ID.
https://grafikart.fr/tutoriels/mon-premier-article-32
And we would like to do the same with our Routing, for that we will declare a new route which will have parameters put between braces.
use Illuminate\Http\Request;
Route::get('/blog/{slug}-{id}', function () {
// ...
});
If now I want to retrieve these parameters from the URL, I can do that using my function’s parameters.
use Illuminate\Http\Request;
Route::get('/blog/{slug}-{id}', function (string $slug, string $id) {
// ...
});
If parameters are used, regular expressions can also be used to constrain the expected format using the method where
.
use Illuminate\Http\Request;
Route::get('/blog/{slug}-{id}', function (string $slug, string $id) {
// ...
})->where(['id' => '[0-9]+', 'name' => '[a-z0-9\-]+']);
Named road
When we define a route in Laravel we have the possibility to name it using the method name()
.
use Illuminate\Http\Request;
Route::get("https://grafikart.fr/blog", function () {
})->name('blog.index');
Route::get('/blog/{slug}-{id}', function (string $slug, string $id) {
// ...
})->name('blog.show');
This naming then makes it possible to generate links automatically using the method route()
.
route('blog.index'); // "https://grafikart.fr/blog"
route('blog.show', ['id' => 10, 'slug' => 'mon-article-test']); // /blog/mon-article-test-10
Route group
If several routes have common information it is possible to group them together.
Route::prefix("https://grafikart.fr/blog")->name('blog.')->group(function () {
Route::get("https://grafikart.fr/", function () {
// ...
})->name('index');
Route::get('/{slug}-{id}', function (string $slug, string $id) {
// ...
})->name('show');
});