How To Get HTTP Hostname In Laravel 8 Tutorial

Share this Article
Reading Time: 2 minutes
438 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.


Premium Courses Running Out, Grab Now!

Master the Coding Skills to Become an Expert in Web Development. A lifetime of knowledge at your fingertips.

Enroll in your favourite web development courses risk-free at $4 only.


Learn More –

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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.

Buy Me a Coffee

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.

Find More on Laravel 8 Articles here