How To Get HTTP Hostname In Laravel 8 Tutorial

Reading Time: 2 minutes
4,003 Views

Inside this article you will get the idea to get HTTP Hostname in Laravel 8. I was looking here and there over internet when I was working on it.

But here, we found a solution to you guys to get http hostname in laravel 8. This is actually not laravel version specific even you can use it any version.

Hostname refers to the name of domain or subdomain from where HTTP request has been initiated. By using request object or request helper function we can get the information of HTTP hostname etc.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Example #1 – Get HTTP Hostname [getHttpHost()]

Get HTTP Hostname in views or in controllers using request helper function –

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

Example #2 – Get HTTP Hostname [getHost()]

Get HTTP Hostname in views or in controllers using request helper function –

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

Example #3 – Get HTTP Hostname

Get HTTP Hostname in controllers using request object –

//...

public function anyControllerMethod(Request $request) { 
    
     $host = $request->getHttpHost(); 
 
    dd($host); 
}

We hope this article helped you to How To Get HTTP Hostname In Laravel 8 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.