Routing in Laravel

In Laravel, all requests are mapped with the help of routes. Basic routing routes the request to the associated controllers. This chapter discusses routing in Laravel.

Laravel includes following Routing −

1). Basic Routing

All Routes are registered in routes (routes\web.php) file,which tells Laravel for the URIs.

The default route is (in routes\web.php):

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

I) When the root URL will execute of the application.

II) The URL will match with the method in the web.php file and it will execute the related function.

III) The function calls the template file resources/views/welcome.blade.php.

All the application routes are registered within the app/routes.php file. This file tells Laravel for the URIs it should respond to and the

associated controller will give it a particular call. The sample route for the welcome page can be seen as shown in the screenshot given below −

2)  Route parameters

In some cases, you need to get the parameters which passed with the URL like:

Route::get('UserID/{id}',function($id) {
   echo 'UserID: '.$id;
});

3). Named Routes

Name routes can be specified using name method onto the route definition Like:

Route::get('user/userprofile', 'UserController@userProfile')->name('userprofile');

User controller will call the function userProfile with parameter as ‘userprofile’

Leave a Reply

Your email address will not be published. Required fields are marked *