in In Laravel, all requests are outlined with the guidance of routes. All Laravel routes are determined in the file located as the app/Http/routes.php file, which is automatically stored and loaded by the framework. Primary routing named as basic routing routes the call may be referred as a request to the associated controllers.
Routing in Laravel includes the following categories −
Basic Routing
Route parameters
Named Routes
Basic Routing
All Laravel routes are defined in the app/Http/routes.php file, which is automatically loaded by the framework. The most basic Laravel routes simply accept a URI and a Closure.
This file tells Laravel for the URIs it should respond to and the associated controller will give it a particular call.
Route::get('home', function () {
return 'Hello World';
});
Example :- app/Http/routes.php
Route::get('/', function () {
return view('welcome');
});
when you simply run your laravel project url . it call the default url . and view the welcome page.
resources/view/welcome.blade.php
Laravel
My Laravel 5
Step 1 − First, we need to execute the root URL of the application.
Step 2 − The executed URL will match with the appropriate method in the route.php file. In our case, it will match to get the method and the root (‘/’) URL. This will execute the related function.
Step 3 − The function calls the template file resources/views/welcome.blade.php. The function later calls the view() function with argument ‘welcome’ without using the blade.php. It will produce the following HTML output.
Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the match method. Or, you may even register a route that responds to all HTTP verbs using the any method:
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('foo', function () {
//
});
Route Parameters :-
Required Parameters -
sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user's ID from the URL. You may do so by defining route parameters:
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
You may define as many route parameters as required by your route:
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
//
});
Route parameters are always encased within "curly" braces. The parameters will be passed into your route's Closure when the route is executed.
Note: Route parameters cannot contain the - character. Use an underscore (_) instead.
Whatever argument that we pass after the root URL (http://localhost:8000/ID/5), it will be stored in $id and we can use that parameter for further processing but here we are simply displaying it. We can pass it onto view or controller for further processing.
Optional Parameters :-
Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:
Route::get('user/{name?}', function ($name = null) {
return $name;
});
Route::get('user/{name?}', function ($name = 'John') {
return $name;
});
There are some parameters which may or may not be present in the URL and in such cases we can use the optional parameters. The presence of these parameters is not necessary in the URL. These parameters are indicated by “?” sign after the name of the parameters. Here is the sample coding for routes.php file for that purpose.
// First Route method – Root URL will match this method
Route::get('/', function () {
return view('welcome');
});
// Second Route method – Root URL with ID will match this method
Route::get('ID/{id}',function($id){
echo 'ID: '.$id;
});
// Third Route method – Root URL with or without name will match this method
Route::get('/user/{name?}',function($name = 'Virat Gandhi'){
echo "Name: ".$name;
});
Step 1 − Here, we have defined 3 routes with get methods for different purposes. If we execute the below URL then it will execute the first method.
http://localhost:8000
Step 2 − After successful execution of the URL, you will receive the following output −
Optional Parameters
Step 3 − If we execute the below URL, it will execute the 2nd method and the argument/parameter ID will be passed to the variable $id.
http://localhost:8000/ID/5
Step 4 − After successful execution of the URL, you will receive the following output −
ID 5
Step 5 − If we execute the below URL, it will execute the 3rd method and the optional argument/parameter name will be passed to the variable $name. The last argument ‘Virat’ is optional. If you remove it, the default name will be used that we have passed in the function as ‘Virat Gandhi’
http://localhost:8000/user/Virat
Step 6 − After successful execution of the URL, you will receive the following output −
Name Virat
Note − Regular expression can also be used to match the parameters.
Named Routes
Named routes allow a convenient way of creating routes. The chaining of routes can be specified using name method onto the route definition.
Route::get('user/profile', function () {
//
})->name('profile');
You may also specify route names for controller actions:
which name is defined in the form action route name is similar to name to ruote.php name('same name');
example :
if you use route() method for link then you must specify the name in the route.php file .
Route::get('posts/{post}/comments/{comment}', 'CommentController@show')->name('commentshow'); Comment show
Routing
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('index');
});
//Route::get('/aboutus', function () { return view('aboutus');});