Laravel - Routing

Laravel - Routing

In Laravel, all requests are mapped using routes. Base routing routes the request to the associated controllers. This chapter discusses routing in Laravel.

Routing in Laravel includes the following categories −

  • Basic Routing
  • Route Options
  • Named routes

Basic Routing

All application routes are registered in the app/rout.php file . This file tells Laravel the URIs it should respond to and the controller associated with it will give it a specific call. An example route for the welcome page can be seen as shown in the screenshot below:

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

example

Look at the following example to understand more about routing −

app/http/routes.php

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

resources/view/welcome.blade.php

<!DOCTYPE html>
<html>
   <head>
      <title>Laravel</title>
      <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" 
         type="text/css">
      
      <style>
         html, body {
            height: 100%;
         }
         body {
            margin: 0;
            padding: 0;
            width: 100%
            display:table;
            font-weight: 100
            font-family: 'Lato';
         }
         .container {
            text-align: center;
            display:table-cell;
            vertical-align: middle;
         }
         .content {
            text-align: center;
            display: inline-block
         }
         .title {
            font-size: 96px;
         }
      </style>
   </head>
   
   <body>
      <div class="container">
         
         <divclass="content">
            <div class = "title">Laravel 5.1</div>
         </div>
			
      </div>
   </body>
</html>

The routing mechanism is shown in the figure below −

Let us now understand in detail what goes into the routing mechanism −

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

Step 2: The completed URL should now match the corresponding method in the route.php file . In this case, it must match the method and the root ('/') URL. This will execute the associated function.

Step 3 − The function calls the resources/views/welcome.blade.php template file. The function then calls the view() function with the 'welcome' argument without using blade.php .

This will produce HTML output as shown in the image below −

Route Options

Sometimes in a web application, you may need to capture parameters passed in via a URL. To do this, you should change the code in the route.php file .

You can write parameters in route.php file in two ways as described here −

Required parameters

These are the parameters that must be written for the routing of the web application. For example, it's important to grab the user ID from the URL. It can be possible by defining route parameters as shown below −

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

Optional parameters

Sometimes developers may pass parameters as optional, and this is possible by including after the parameter name in the URL. It is important to keep the default value specified as the parameter name. Look at the following example which shows how to define an optional parameter −

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

The example above checks to see if the TutorialsPoint value matches and accordingly directs it to a specific URL.

Named routes

Named routes allow a convenient way to create routes. A chain of routes can be specified using the name method in the route definition. The following code shows an example of creating named routes with a controller −

Route::get('user/profile', 'UserController@showProfile')->name('profile');

The custom controller will call the showProfile function with the parameter as the profile . The parameters use the name method in the route definition.