Home > database >  differences in routing in laravel
differences in routing in laravel

Time:09-26

how you doing, I have just started learning Laravel, and I have question about routing I have written this code

Route::get('/articles/{id}', function(Request $request, $id){
    return 'article id:' . $id;
});

Route::get('/articles/{id}', function($id){
    return 'article id:' . $id;
});


Route::get('/articles/{parameter}', function($id){
    return 'article id:' . $id;
});

the three previous routes give the same Result

and the next two routes give the same result as well


Route::get('/article/{id}', function(Request $request, $id){

    return view('article');
});


Route::get('/article/{id}', function(){
    return view('article');
});

So I do not under stand why should we pass request to routing function since it works without any need of adding the request,

Thanks In Advance.

CodePudding user response:

Passing the $request is optional and you don't even have to pass it, you can just use the request() helper instead.

If you do pass it, it uses Laravel automatic dependency injection.

  • Related