Laravel 7 Tutorial – Web Routes

Laravel 7 Tutorial - Web Routes

Laravel 7 Tutorial – Web Routes



Laravel 7 Tutorial – Web Routes

Laravel Routes:

In Laravel we can define routes. So when you want some page to load when user goes to some url. That’s where laravel routes come into play.





For example if you want that on click of a link user should go to products page you will define a route “/products”. And in “Routes/web.php” it should look something like this



//Products Route 
Route::get('/products', function(){
     //a view file named products will display output
     return view('products');
});

You might add link for user to click to go to this “products” route.





<a href="{{route('/products')}}">Products</a>

Route Types

We can use get or post method in routes. And also we use Controller and function in routes for advanced development. Route in commercial applications should look something like this



//Products Route 
Route::get('/products', [
    'uses' => 'ProductsController@getProductFunction'
]);

Here you can see we have a controller and function. So when user goes to the route “/products”. Controller named “ProductController” which has a function “getProduct” will be used to display view file.





Function in controller will be returning the view so it should look like this



<?php

namespace App\Http\Controllers;

class ProductController extends Controller
{
   public function getProduct(){
        return view('products');  
   }
}

Authorization checking:

we can check if user is authorized to go to the route file before sending user to view file. For that we use laravel policy and middleware. Read about that more here





Important Links:



Php Storm : https://www.jetbrains.com/phpstorm/





Laravel 7 Middleware : https://laravel.com/docs/7.x/middleware



Laravel 7 Policies : https://laravel.com/docs/7.x/authorization