Laravel Routing

 15 - Oct - 2021
 XpertPhp
 26

Routing is one of the basic concepts at Laravel. Routing in Laravel allows you to route all requests for your application to the appropriate controller. there are many ways to create routing in our laravel application. So here we are explaining the following method below.

Laravel Basic Routing

The basic Laravel routes accept a URI and a Closure, providing a simple method to define routes.
Route::get('/', function () {
 return view('welcome');
});

Laravel Route Parameters

Sometimes, we may need to pass parameters in the URL. there are two types of parameter routes in laravel. such as Required Parameters and Optional Parameters.

Required Parameters

Route::get('/post/{id}', function($id)  
{  
  return "post id is : ". $id;   
});

Optional Parameters

Route::get('post/{name?}', function ($name=null) {  
    return $name;  
});

Laravel Named Routes

The named routes give you the ability to reference routes when redirecting to specific routes.
Route::get('post/add', 'PostController@add')->name('post-add');

Group Routes

The group routes allow you to group all the routes. if you want to use the same attribute for all the routes then you can use the group routes.
Route::group(['namespace' => 'Admin'], function()
{
    Route::get('/post',function(){  
      echo "post";  
    });  

});