- 2 years ago
- Zaid Bin Khalid
- 1,604 Views
-
3
In Laravel, we plot all requests with the help of routes. Basic routing routes the request to the related controllers. We learn about routing in Laravel in this chapter.
Routing has the following categories in Laravel.
- Basic Routing
- Route parameters
- Named Routes
Basic Routing
In this, we enter all the application routes within the app/routes.php file. This file informs Laravel for the URIs it should answer, and the related controller will give it a specific call. You can see the sample route for the welcome page in the screenshot below.
Route::get ('/', function () {
return view('welcome');
});
Example
Let’s see the following example to learn about routing better.
app/Http/routes.php
Route::get('/', function () {
return view('welcome');
});
resources/view/welcome.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>
<link href = "https://fonts.googleapis.com/css?family=Lato:100" rel = "stylesheet"
type = "text/css">
<style>
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
width: 100%;
display: table;
font-weight: 100;
font-family: 'Lato';
}
.container {
text-align: center;
display: table-cell;
vertical-align: middle;
}
.content {
text-align: center;
display: inline-block;
}
.title {
font-size: 96px;
}
</style>
</head>
<body>
<div class = "container">
<div class = "content">
<div class = "title">Laravel 9.0</div>
</div>
</div>
</body>
</html>
Now let us understand the steps closely required in the routing mechanism.
Step 1: We should execute the root URL of the application first with the proper method.
Step 2: After this, the executed URL should match the related method in the route.php file, if you didnt get the data on your route URL just type below command and try again by refreshing the web page.
php artisan optimize
php artisan route:clear
php artisan config:clear
php artisan view:clear
In the present case, it should match the method and the root (‘/’) URL. This will perform the related function.
Step 3: The function calls the template file resources/views/welcome.blade.php. Next, the function calls the view() function with the argument ‘welcome’ without using the blade.php.
It will produce the HTML output as shown in the image below.
Route Parameters
You may need to show the parameters passed with the URL sometimes in the web application. For this, you should change the code in the routes.php file.
You can show the parameters in the routes.php file in two ways, as discussed here.
Required Parameters
These parameters are those which should be compulsorily shown for routing the web application. It is important to show the user’s identification number from the URL, for example. It is only possible by defining route parameters as shown below.
Route::get('ID/{id}',function($id) {
echo 'ID: '.$id;
});
Optional Parameters
You can pass extra parameters in your route or one parameter. For example if you going to edit the page you can set the page ID in your route or name in your route. Actually all the routes by default send all the REQUEST parameters and your get.
Route::get('user/{name}', function ($name = 'Hi Route') { return $name;});
The example above checks if the value matches to Hi Route and correctly routes to the defined URL.
Named Routes
Named routes allow an easy way of making routes. The chaining of routes can be stated using the name method onto the route definition. The following code shows an example to make named routes with the controller.
Route::get('user/profile', 'UserController@userProfile')->name('profile');
The user controller will call for the function userProfile with parameter as a profile. The parameters use the name method in the route definition.
- 2 years ago
- Zaid Bin Khalid
- 1,604 Views
-
3