Laravel Get HTTP hostname with Example

Today, We want to share with you Laravel Get HTTP hostname with Example.In this post we will show you laravel 6.2 get domain with http, hear for laravel get domain with http we will give you demo and example for implement.In this post, we will learn about laravel get domain without http with an example.

Laravel Get HTTP hostname with Example

There are the Following The simple About laravel 6 get domain Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel get server hostname, so the How to get http hostname in Laravel? is used for this example is following below.

Get all header request in Laravel

use getHttpHost() and getHost() methods.

$request_hosts = request()->headers();
dump($request_hosts);

using request getHttpHost

$request_host = request()->getHttpHost();
dump($request_host);

using request methods

$request_host = request()->getHost();
dump($request_host);

using Laravel 6 Controller

public function storeGames(Request $request) {

    $request_host = $request->getHttpHost();
    dump($request_host);

}

How To Get HTTP Hostname In Laravel?

In Laravel, you can get the HTTP hostname (i.e. the domain name) using the request() function. Here’s an example:

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

This will return a string containing the HTTP hostname, including any subdomains and the top-level domain. For example, if the user is visiting https://www.example.com, this code will return the string “www.example.com”.

If you want to extract just the top-level domain (i.e. the part after the last dot), you can use the parse_url() and explode() functions to split the hostname into an array and extract the last element:

$hostname = request()->getHttpHost();
$parts = explode('.', parse_url('http://' . $hostname, PHP_URL_HOST));
$tld = end($parts);

In this example, we first use parse_url() to add the http:// scheme to the hostname, which is required by explode(). Then we use explode() to split the hostname into an array of its subdomains and top-level domain. Finally, we use end() to extract the last element of the array, which is the top-level domain. For example, if the user is visiting https://www.example.com, this code will return the string “com”.

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel get http host, laravel getHttpHost().
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment