Backend Development 4 min read

Understanding Laravel Request Host Methods: host(), httpHost(), and schemeAndHttpHost()

This article explains Laravel's three request host methods—host(), httpHost(), and schemeAndHttpHost()—detailing their differences, usage examples, and how to apply them in multi‑tenant applications to dynamically generate tenant‑specific URLs.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding Laravel Request Host Methods: host(), httpHost(), and schemeAndHttpHost()

When developing web applications, understanding request information, especially the host, is essential. Laravel's Request object offers three convenient methods— host() , httpHost() , and schemeAndHttpHost() —to retrieve different parts of the host data.

Mastering Laravel Request Host Methods

Laravel provides three methods to obtain the incoming request's host information:

host(): returns the request's hostname.

httpHost(): returns the HTTP hostname and port.

schemeAndHttpHost(): returns the full URL scheme and HTTP host.

Using these methods flexibly allows you to acquire the needed host details for more powerful applications.

1. host() method

The host() method returns the request's hostname.

$host = $request->host();

2. httpHost() method

The httpHost() method returns the HTTP hostname and port number.

$httpHost = $request->httpHost();

3. schemeAndHttpHost() method

The schemeAndHttpHost() method returns the complete URL scheme and HTTP host.

$fullHost = $request->schemeAndHttpHost();

Real‑world Example

In a multi‑tenant application, you may need to handle requests from different subdomains and identify the tenant based on subdomain information. Below is a middleware example that uses the above methods to determine the tenant and generate tenant‑specific URLs.

host();
        $tenant = Tenant::where('subdomain', $host)->firstOrFail();
        // Store tenant in request for later use
        $request->merge(['tenant' => $tenant]);
        // Generate URLs for the tenant
        $tenantUrls = [
            'dashboard' => $request->schemeAndHttpHost() . '/dashboard',
            'api' => $request->schemeAndHttpHost() . '/api/v1',
        ];
        $request->merge(['tenantUrls' => $tenantUrls]);

        return $next($request);
    }
}

In a controller you can then access the tenant and URLs:

public function dashboard(Request $request)
{
    $tenant = $request->tenant;
    $dashboardUrl = $request->tenantUrls['dashboard'];
    return view('dashboard', compact('tenant', 'dashboardUrl'));
}

The response might look like:

// GET http://client1.ourapp.com/dashboard
{
    "view": "dashboard",
    "data": {
        "tenant": {
            "id": 1,
            "name": "Client 1",
            "subdomain": "client1"
        },
        "dashboardUrl": "http://client1.ourapp.com/dashboard"
    }
}

By using host() to parse the subdomain and schemeAndHttpHost() to build full URLs, each tenant can be correctly identified and accessed.

multi-tenantPHPLaravelRequestHost
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.