Laravel Routes are the paths that lead to somewhere on the website. For instance, mraheelkhan.com/contact the contact part is URI and it leads to a Hello World string that returns a string on a web page which is called closure in the Laravel route.
1: Route class and get method
The Route is Laravel class that has HTTP verbs including GET
, POST
, PUT
, PATCH
, and DELETE
methods.
GET
is a normal web browser method of request, whenever we write an URL or search for something in the browser it will generate a GET request.
For instance, you are currently browsing mraheelkhan.com/slug-or-uri-of-post-you-are-reading-now is opened via a GET request made by your click to my blog.
We will look into the GET
, POST
,PUT
, PATCH
, and DELETE
methods later in the tutorials.
2: URI part of route
The URI is the actual part that can be seen on the URL. As mentioned earlier, /contact part is URI which is a path on which the request is running.
To make it more clear, let\'s discuss it in layman\'s words.
When you visit a supermarket you will find a lot of paths where you will get different items including households, foods, groceries, fruits and other stuff.
Each row and shelf would have a tag of a relevant category. In the above supermart image, you will see that there are two different rows and shelves, each has relevant path cookies and groceries.
So, cookies and groceries are the URI and paths of the supermarket/URL. Each will lead to different content and items.
3: Call back or closure function
Whenever we declare a path, it will always have a closure that can be a static method like the above function or it can target a specific function within a controller. We will dive into the controller and function later in the tutorial.
Route::get(/greetings, function(){
return Greetings;
});
Route::get(/greetings, [HomeController::class, index]);
4: Returned content from closure function
We will return something in the closure method. In the above example, we are returning a string Greetings. Here we can return variable, array, and/or HTML pages.
Available Router Methods (HTTP Verbs)
<pre class="wp-block-code">```
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
These are the available router methods that we can use as per need in our Laravel application.
The GET
method is almost everywhere.
POST method is used when we want to secretly send data to the backend Laravel. For example, to submit a login or registration form.
PUT AND PATCH methods are used for updating purposes. For example, you want to update the email of an existing user.
DELETE is used for destroying an entire record from the database.
Laravel View Routes
Sometimes we want to return a view directly from routes in order to show an HTML page.
To return a view directly from web.php routes page we need to use Route::view() method from the Route class.
Route::view('/welcome', 'welcome');
Whereas, the /welcome
is the URI or path of URL and callback welcome is the name of view blade file available in resources/views/*
directory by the name of welcome.blade.php
Laravel Route Parameters
Our Laravel application will always require dynamic parameters in the URI
. Because we always require dynamic data based on those parameters.
For example, if we want to fetch the user records based on user id by passing it within the URL.
mraheelkhan.com/users/1
user id = 1 in the above URL.
By accessing /user/1 URL it will return
User 1
Laravel Named Routes
To make all those routes and URLs developer-friendly and easy to remember, the Laravel provides named routes.
We can name the URL in order to remember it and use it dynamically.
There are two major benefits of using named routes.
- Easy to remember: You will easily remember which URL has what name.
- Dynamic URL: If you want to change
/users
url to/profiles
, all you have to change the path only and it will be dynamically updated everywhere in the application because you will use the name of Route not the exact url. So think of it like a variable, if you change its value the changes will appear everywhere.
When you want to call or use these URLs. You can simply write route(route_name)
.
{{ route(users.index) }}
/* Output = mraheelkhan.com/users */
{{ route(users.edit, 1) }}
/* Output = mraheelkhan.com/users/1 */
Laravel Route Grouping
Sometimes we want to group together routes because of the same nature or use.
For example, there are 3 different routes available for users purposes.
- /users
- /users/{id}
- /users/{id}/edit
You can see that I am repeatedly using /users in all these 3 routes.
There is an option that can help to achieve a standard that says Don\'t repeat yourself.
I will group all these routes with prefix users.
There are other helpers methods available on Route class including middleware
, name
, etc.