Get domain name in laravel

There are lots of method available in laravel to get the domain name. You can get domain name in laravel using getHost() and getHttpHost() method on request() helper.

Answers 1
  • Get domain name in laravel using web.php file

    $host = request()->getHttpHost();
    

    Output :

    w3codegenerator.com
    $getHost = request()->getHost();
    

    Output :

    w3codegenerator.com
    $hostwithHttp = request()->getSchemeAndHttpHost();
    

    Output :

    https://w3codegenerator.com 

    Code Example routes\web.php

    Route::get('/get-domain', function(){
        $host = request()->getHttpHost();
        echo $host ."<br/>";
        $getHost = request()->getHost();
        echo $getHost ."<br/>";
        $hostwithHttp = request()->getSchemeAndHttpHost();
        echo $hostwithHttp ."<br/>";
    });
    
    0

    Copy/Paste code in your web.php file and it will return the domain name in laravel on visit http://localhost:8000/get-domain . You can use this code snippet in view and as well as controller file.

  • Back to code snippet queries related laravel