Recently, while working on my Laravel 10 application, I encountered the need to retrieve the hostname. Despite going through the entire documentation on requests, I couldn't pinpoint where to obtain the hostname in my controller.
I discovered a solution for this challenge. You can effortlessly obtain the HTTP hostname from the request object using two helpful methods: getHttpHost()
and getHost()
. These methods not only retrieve the domain name but also allow you to obtain subdomain names effortlessly.
Get Http Hostname in Laravel
You can retrieve it as follows:
Example 1 :
$host = request()->getHttpHost();
Example 2 :
$host = request()->getHost();
Example 3 :
public function yourControllerFun(Request $request)
{
$host = $request->getHttpHost();
dd($host);
}
I'm here to help! If you have any more questions or if there's anything else I can assist you with, feel free to let me know.