How To Get HTTP Hostname In Laravel 10

30-May-2023

.

Admin

How To Get HTTP Hostname In Laravel 10

Hi Dev,

Today, I would like to show you how to get an HTTP hostname in laravel 10. This article will give you a simple example of getting an HTTP hostname. Let’s discuss laravel 10 on how to get an HTTP hostname. This tutorial will give you a simple example of laravel 10 HTTP hostname. Let's see the below example laravel 10 gets an HTTP hostname.

A very few days ago I was trying to get the hostname in my laravel 10 app. I read the whole document of requests but I didn't get where to get the hostname in my controller.

But I found a solution for that we can easily get http host name from the request object and helper that way you can easily get the domain name or subdomain name too. request provide two methods to get the hostname as getHttpHost() and getHost().

Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip the following step.

composer create-project laravel/laravel example-app

You can get like as bellow:

Example: 1

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

Example: 2

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

Example: 3

public function yourControllerFun(Request $request) {

$host = $request->getHttpHost();

dd($host);

}

It will help you...

#Laravel 10