How to Get Http Hostname in Laravel?
Hello Dev,
This tutorial is focused on how to get http hostname in laravel. This article goes in detailed on how to get hostname in laravel. Here you will learn laravel request hostname. step by step explain laravel get hostname. So, let us see in detail an example.
In this example, we will use using getHost() or getHttpHost() request method to get hostname in laravel. we will see simple example with output.
1. getHost(): getHost() is used to retrieve the HTTP host from the "Host" header of the incoming request. The "Host" header is a part of the HTTP request headers sent by the client (e.g., a web browser) and specifies the hostname of the server to which the client is making the request.
It returns the host as a string, without the scheme (http or https) or any port information. If you want to get just the hostname, this method is useful.
2. getHttpHost(): getHttpHost() is also used to retrieve the HTTP host from the "Host" header of the incoming request, but it includes the scheme (http or https) and port information, if applicable.
It returns the host as a complete URL, which includes the scheme and port number. This method can be helpful when you need the complete URL, including the protocol and port.
Let's see the one by one example:
Example 1: Laravel Get Hostname using getHost()
<?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: localhost
Example 2: Laravel Get Hostname using getHttpHost()
<?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: http://example.com:8080
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Connect PostgreSQL Database in Laravel?
- Laravel Blade Foreach Last Element Example
- Laravel Blade Foreach First Element Example
- How to Encrypt Decrypt Database Fields in Laravel?
- Laravel Relationships with Optional() Helper Example
- How to Send Mail using PHPMailer in Laravel?
- Laravel Migration Change Default Value Example
- Laravel 10 Change Date Format Examples
- Laravel 10 Ajax Request Example Tutorial
- How to Get All env Variables in Laravel?
- Laravel - How to Check If Request Input Field is Empty or Not?
- Laravel Vue JS Axios Post Request Example
- How to Check Request is Ajax or Not in Laravel?
- How to Check Request Method is GET or POST in Laravel?