Laravel Routing Tutorial

Laravel routing is an important topic of the laravel framework. The main functionality of the laravel routes is to redirect or map all application requests to the related controller.

Routing acts as a bridge between various HTTP requests and a particular controller.

We can categorize laravel routing in three categories as the following

1. Laravel Basic Routing

2. Laravel Routing Parameters (Route Function with parameter)

3. Laravel Named Routes

4. Laravel Wildcard For Subdomains

1. Laravel Basic Routing

Laravel automatically creates the basic route files when you make a new laravel project. These routes are written inside the app->http->routes.php file.

This app->http->routes.php file contains all the redirect options for each http request.

By default you should have the below coding lines inside the app->http->routes.php file

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

When you call the root URL of the application, the compiler will call the function().

Root URL should like http://localhost:8000 (If you are using localhost instead of the real server)

This function() is returning the welcome.blade.php file. So the system will simply load welcome.blade.php file.

2. Laravel Routing Parameters (Route Function with parameter)

Many times, developers need to catch up the parameters sent along with the URL.

For this purpose, laravel helps us with routing parameters.

Developers can capture the parameters and it’s values using two methods:

  1. Required Parameters
  2. Optional Parameters

Required Parameters

As the name suggests, these parameters are mandatory to be caught by the URL.

For example, when you are passing the route for login URL, parameters like username and password must be passed along with the login URL.

Code example for Required parameters can be written like

Route::get('PWD/{pwd}',function($pwd) {
   echo 'PASSWORD: '.$pwd;
});

Optional Parameters

These are the parameters which are not mandatory. You can pass these parameters only if they are available.

Example of this type of parameters can be contact number, city name, website name etc.

If user has entered these parameters then you should pass them with the URL and you can skip them if they are not entered by the user.

Route :: get ('emp/{cityname?}', function ($cityname= null) {
    echo $cityname;
});

3. Laravel Named Routes(Route Function with parameter)

This method gives a unique way to define the routes in laravel. It allows you to refer to the routes when generating URLs or redirects to the specific routes.

We can provide name to certain parameters using this concept. Below is the example of named parameters

Route::get('worker/detais', 'WorkerController@showDetais')->name('detais');

In the above code snippet, system will pass the URL with the parameter whose name is “details

You can see that we can use name method in the definition of the route to provide the parameter name.

Making URLs

Once you provide a named route to a specific route, then you can use the name of the route when generating URLs or redirecting through a global route function.

    //Making URLs  
    $url= route('worker_details');  
    //MakingRedirects...  
    return redirect() -> route('worker_details');  

When our URL has many parameters, it will become very lengthy. In this case, named route help us to shorten the URL.

We can assign a short name to the URL and then we can use this short name in the second parameter of get() function.

    Route::get('worker/details/allinformation',array   
    ('as'=>'worker.details',function()  
    {  
       $url=route('worker.details');  
       return "Actual url is : " .$url;  
    }));  

Using the named route, we can also handle the click events of the links.

Let us see, how we can do this. First of all, go to the web.php file and add the below coding lines.

    Route::Get('/',function()  
    {  
      return view('worker');  
    });  
      
    Route::get('worker/details',function()  
    {  
      $url=route('worker.details');  
     return $url;  
    })->name('worker.details');  

Now we need to create one new blade php file and give it a name as worker.blade.php

Add the below line inside worker.blade.php file.

    <a href="{{ route('student.details') }}">Click on Worker</a>  

Above code will create one link with the name as “Click on Worker

When the user clicks it, it will open the worker.details page which is named route.

4. Laravel Wildcard for Subdomains

For handling the subdomain routes, laravel enable us with a feature called a wildcard.

Let’s see how we can handle subdomain like peter.laravelpro.com

Code in web.php file is as the below

Route::domain('{company_name}.workspace.com')->group(function () {
    Route::get('users', 'UsersController@index');
});

So {company_name} could be any value (of course, you need to configure it in your domain DNS records, too), and then this will come as a parameter variable to the Controller, with the same name.

public function index($company_name)
{
    $company = Company::findOrFail($company_name);
    $users = User::where('company_id', $company->id)->get();
    return view('users.index', compact('users'));
}