Laravel-Routing


Routes are the important part of all websites. in laravel, all URL request is mapped with the help of routes. This chapter discusses routing in laravel. Routing in laravel has three categories.

  • Basic Routing
  • Routing with Parameter
  • Named Routing

Basic Routing

All the applying routes area unit registered among the app/routes.php file. This file tells Laravel for the URIs it should reply to and also the associated controller can provide it a selected decision. The sample route for the welcome page will be seen as shown within the screenshot given below.

Routing in laravel

Open "web.php" file by default you will find the welcome route in this page.

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

 

resources/view/welcome.blade.php

when you visit this page you will find the welcome page.


      
         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;
         }
     

Let us now understand the steps of the routing mechanism step by step...

Step:1

Initially, we are going to run root URL of our Laravel Application.

 

Step:2

Now, the Executed URL should match with the suitable method within the route.php file. within the present case, it should match the method and of the root (‘/’) address. this will execute the related function.

 

Step:3

Next, The function calls the view file from resources/views/welcome.blade.php. Next, the function calls the view() function with ‘welcome’ while not add the blade.php after the file name. after run Application will you show in the image below −

Welcome page of laravel

Route Parameters

This type of routing is important for when we want to pass some value or variables by URL to one page to another page. 

Route Parameter

We can see the above example passed four parameters. we have two ways by which we can capture the parameters passed with the URL.

  1. Required Parameters
  2. Optional Parameters

Required Parameters

This type of parameters is mandatorily captured for routing the web application. For example, it is important to pass the user’s ID number from the URL.

 

Route::get('email/{email}',function($email){
   echo 'EMAIL: '.$email;
});

 

Optional Parameters

Optional Parameters are not mandatorily like Required Parameter. sometimes the developer can pass Parameters as optional and it is possible with the inclusion of ? after a parameter name in URL.

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

The example above checks if the value matches to Codermen and accordingly routes to the defined URL.

Named Routes

Named Routes are allowing to the convenient way of creating routes. we can create specified name method onto the route definition. The following code shows an example of a Named route.

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

In this is Example my route name is "profile"  and we call the showProfile function with parameter profile.