7 Ways to Get Your Domain Name (Host) in Laravel

laravel
tutorial
domain
host
Nabil Hassen
Nabil Hassen
Jul 31, 2025
7 Ways to Get Your Domain Name (Host) in Laravel
Last updated on Jul 31, 2025
Table of contents:

Accessing domain name (host) in Laravel

In Laravel, there are several ways to retrieve the domain or base URL of your web application. Depending on the context (e.g., within a controller, view, or service), one method might be more appropriate than another. Here’s a rundown of different methods to get the domain or host in Laravel.

1. config('app.url')

This returns the base URL set in your .env file as APP_URL.

// .env
APP_URL=https://example.com
 
// Usage
$url = config('app.url');
echo $url; // https://example.com

This is useful when you want to get the domain as configured in your environment settings.

2. $request->host()

This method returns only the host (domain without scheme or port).

public function show(Request $request)
{
return $request->host(); // example.com
}

Use this when you only need the domain name.

3. $request->httpHost()

This returns the host including the port number, if present.

public function show(Request $request)
{
return $request->httpHost(); // example.com or example.com:8000
}

It’s helpful when dealing with custom ports during development.

4. $request->schemeAndHttpHost()

This returns scheme (http or https), the host, and the port number.

public function show(Request $request)
{
return $request->schemeAndHttpHost(); // https://example.com or https://example.com:8000
}

Use this when you need the full base URL including the scheme.

5. url('')

This helper returns the base URL of the application.

echo url(''); // https://example.com

It’s simple and handy, especially inside views or Blade templates.

6. URL::to('')

The URL facade offers similar functionality to the url() helper.

use Illuminate\Support\Facades\URL;
 
echo URL::to(''); // https://example.com

It’s a good alternative if you prefer using facades.

7. Request::root()

This static call returns the root URL based on the current request.

use Illuminate\Support\Facades\Request;
 
echo Request::root(); // https://example.com

This is useful when you’re outside of controller methods but still want to access the current request context.

Final Notes

Choose the method that best fits your use case:

  • Use config('app.url') for environment-based settings.
  • Use $request->... methods when working inside HTTP request context.
  • Use helpers or facades when outside of controllers or when writing reusable code.

Understanding these options helps you write more flexible and context-aware Laravel applications.

Nabil Hassen
Nabil Hassen
Full Stack Web Developer

Stay Updated.

I'll you email you as soon as new, fresh content is published.

Thanks for subscribing to my blog.

Latest Posts