How to Get Http Hostname in Laravel

  Mar 2024
  ITSolutionsGuides
  Category: Laravel
How to Get Http Hostname in Laravel

Welcome To ITSolutionsGuides,


In this topic we will see how to get the host name of the any incoming request in laravel using the getHost() and getHttpHost() method. This methods will get the host name of any of the incoming request to the application and give us the host name of the request.
To retrieve the HTTP hostname in Laravel, utilize the getHttpHost() or getHost() method within the request object. This function seamlessly fetches the hostname, simplifying tasks such as dynamic URL generation or handling multi-domain applications in Laravel with ease.

1) getHost() this method is used to retrive the host name of the incoming request , it will get the host name of the incoming request and then gives the host name of the request. IT will not give the complete information of the incoming host name.

2) getHttpHost() this method is also used to retrive the host name of the incoming request, unlike the previous method it will give the complete information about the host name of the incoming request.

Let's Start Coding

By using the getHost() method it will give the doamin name ,

<?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $hostname = $request->getHost();
  
        return "HTTP Hostname: " . $hostname;
    }
}


-------------
output

HTTP Hostname: itsolutionsguides

Let's Start Coding

using getHttpHost() we will get the entire host name or domain or url of the website unlike previous example it will only give domain name only not the complete url,

<?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $hostname = $request->getHttpHost();
  
        return "HTTP Hostname: " . $hostname;
    }
}


-------------
output

HTTP Hostname: https://itsolutionsguides.com

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides