Laravel - Routing

Laravel - Routing :

Laravel, all requests are mapped with the help of routes. Basic routing routes the request to the associated controllers.

Routing in Laravel includes the following categories −

  • Basic Routing
  • Route parameters
  • Named Routes

Basic Routing

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 app/Http/routes.php

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


Step 1 − Initially, we should execute the root URL of the application.

Step 2 − Now, the executed URL should match with the appropriate method in the route.php file. In the present case, it should match the method and the root (‘/’) URL. This will execute the related function.

Step 3 − The function calls the template file resources/views/welcome.blade.php. Next, the function calls the view() function with argument ‘welcome’ without using the blade.php.

Route Parameters

Often in the application, we intend to capture the parameters passed with the URL. To do this, we need to modify the code in routes.php file accordingly. There are two ways by which we can capture the parameters passed with the URL.

You can capture the parameters in routes.php file in two ways as discussed here −

Required Parameters

These parameters are those which should be mandatorily captured for routing the web application. For example, it is important to capture the user’s identification number from the URL. This can be possible by defining route parameters as shown below −

Optional Parameters Sometimes developers can produce parameters as optional and it is possible with the inclusion of ? after the parameter name in URL. It is important to keep the default value mentioned as a parameter name. Look at the following example that shows how to define an optional parameter −
  • Route::get('user/{name?}', function ($name = 'Welookups') { return $name;});
  • The example above checks if the value matches to Welookups accordingly routes to the defined URL.