A web.php file is available to set the route of the website in Laravel framework. In this file, we define all the routes in the website here. If we want to create an API, then we set the route in the api.php file.
// basic routing
Route::get('/index', function(){
echo "index page";
});
// create simple route
Route::get('/index',[HomeController::class,'index']);
In this file, we set the route in multiple ways.
In most of the cases, we set GET and POST route only. In the first parameter, we set the value of the URL set. Then in the second parameter, we specify the name of the controller and the name of the function.
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
If we want to redirect to any URL then we set redirect route. In this 2 parameters have to be set. In the first parameter, the URL is set and in the second parameter, where to redirect, this URL has to be set that URL.
Route::redirect('/index','/sitedown-mode');
Route::redirect('/here', '/there', 301);
Route::permanentRedirect('/here', '/there');
If we want to see the view file directly from the URL, then we use the view root. The URL in the first parameter and the name of the view file in the second parameter have to be passed. Pass the variables (key value pair) in the third parameter.
// create view route
Route::view('/direct-view','direct-view');
// create view route with parameter
Route::view('/direct-view','direct-view',['name' => 'deepak tailor']);
When we create dynamic URL we pass some value in value like user id or product id, project slug url.
http://127.0.0.1:8000/products/258485
Route::get('products/{product_id}',function($product_id){
return 'Product Id - '.$product_id;
});
Route::get('products/{product_id}',[ProductController::class,'view_product']);
Route::get('products/{product_id}/user/{user_id}',function($product_id, $user_id){
echo 'Product Id - '.$product_id;
echo 'User id - '.$user_id;
});
Route::get('products/{product_id}/user/{user_id}',[ProductController::class,'view_product_by_user']);
When we create dynamic url we pass some value in value and sometimes parameter is not available then we can set those routes with optional parameter.
http://127.0.0.1:8000/products
Route::get('products/{product_id?}',function($product_id = '0'){
echo 'Product Id - '.$product_id;
});
We can give names to all the routes we set. So that we can call route by direct name in view file.
Route::get('/index-page',function(){
echo 'index page';
})->name('home.page');
Route::get('/index-page',[HomeController::class,'index'])->name('home.page');
Route::get('products/{product_id?}',function($product_id = '0'){
echo 'Product Id - '.$product_id;
})->name('product.details');
We can also use middleware in routes. If some route is to be protected then we use middleware.
// set route with using middleware
Route::group(['middleware'=>'verify_users'],function(){
Route::get('user-profile',function(){
echo 'protected route';
});
});
Route::group(['middleware'=>'verify_users'],function(){
Route::get('user-profile',[UserController::class,'user_profile']);
});
Route::group(['middleware'=>'verify_users'],function(){
Route::get('user-profile',[UserController::class,'user_profile'])->name('user.profile');
});
Route::middleware(['verify_users'])->group(function(){
Route::get('user-profile',[UserController::class,'user_profile']);
});
Route::middleware(['verify_users'])->group(function(){
Route::get('user-profile',[UserController::class,'user_profile'])->name('user.profile');
});
In Routes, we can create a group of controllers so that we will not have to write to the controller again and again.
Route::controller(UserController::class)->group(function(){
Route::get('user-profile','user_profile');
Route::get('change-password','change_password');
});
We can put prefix in the routes. If some URL has to prefix it, then prefix route is used.
Route::group(['prefix'=>'v2'],function(){
Route::get('/submit-document',[DocumentController::class,'submit_document'])->name('submit.document');
});
Route::prefix('v2')->group(function(){
Route::get('/submit-document',[DocumentController::class,'submit_document'])->name('submit.document');
});
My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in