Skip to content

[12.x] Add domain() and subdomain() methods to Illuminate\Support\Uri class #56326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/Illuminate/Support/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,50 @@ public function host(): ?string
return $this->uri->getHost();
}

/**
* Get the subdomain from the URI.
*/
public function subdomain(): ?string
{
if (! $host = $this->host()) {
return null;
}

if (filter_var($host, FILTER_VALIDATE_IP)) {
return null;
}

if (count($parts = explode('.', $host)) < 3) {
return null;
}

return $parts[0];
}

/**
* Get the domain from the URI.
*/
public function domain(): ?string
{
if (! $host = $this->host()) {
return null;
}

if (filter_var($host, FILTER_VALIDATE_IP)) {
return null;
}

if (count($parts = explode('.', $host)) < 2) {
return null;
}

if (count($parts) >= 3) {
return implode('.', array_slice($parts, 1));
}

return $host;
}

/**
* Get the URI's port.
*/
Expand Down
93 changes: 93 additions & 0 deletions tests/Support/SupportUriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,99 @@ public function test_path_segments()
$this->assertEquals(3, $uri->pathSegments()->count());
}

public function test_subdomain_extraction()
{
$uri = Uri::of('https://api.laravel.com');
$this->assertEquals('api', $uri->subdomain());

$uri = Uri::of('https://www.laravel.com');
$this->assertEquals('www', $uri->subdomain());

$uri = Uri::of('https://admin.dashboard.laravel.com');
$this->assertEquals('admin', $uri->subdomain());

$uri = Uri::of('https://laravel.com');
$this->assertNull($uri->subdomain());

$uri = Uri::of('https://example.org');
$this->assertNull($uri->subdomain());

$uri = Uri::of('https://192.168.1.1');
$this->assertNull($uri->subdomain());

$uri = Uri::of('http://127.0.0.1:8080');
$this->assertNull($uri->subdomain());

$uri = Uri::of('http://localhost');
$this->assertNull($uri->subdomain());

$uri = Uri::of('http://localhost:3000');
$this->assertNull($uri->subdomain());

$uri = Uri::of('/path/only');
$this->assertNull($uri->subdomain());

$uri = Uri::of('');
$this->assertNull($uri->subdomain());

$uri = Uri::of('https://staging.api.laravel.com');
$this->assertEquals('staging', $uri->subdomain());

$uri = Uri::of('https://test-env.laravel.com');
$this->assertEquals('test-env', $uri->subdomain());

$uri = Uri::of('https://example.co.uk');
$this->assertEquals('example', $uri->subdomain());
}

public function test_domain_extraction()
{
$uri = Uri::of('https://api.laravel.com');
$this->assertEquals('laravel.com', $uri->domain());

$uri = Uri::of('https://www.laravel.com');
$this->assertEquals('laravel.com', $uri->domain());

$uri = Uri::of('https://admin.dashboard.laravel.com');
$this->assertEquals('dashboard.laravel.com', $uri->domain());

$uri = Uri::of('https://laravel.com');
$this->assertEquals('laravel.com', $uri->domain());

$uri = Uri::of('https://example.org');
$this->assertEquals('example.org', $uri->domain());

$uri = Uri::of('https://192.168.1.1');
$this->assertNull($uri->domain());

$uri = Uri::of('http://127.0.0.1:8080');
$this->assertNull($uri->domain());

$uri = Uri::of('http://localhost');
$this->assertNull($uri->domain());

$uri = Uri::of('http://localhost:3000');
$this->assertNull($uri->domain());

$uri = Uri::of('/path/only');
$this->assertNull($uri->domain());

$uri = Uri::of('');
$this->assertNull($uri->domain());

$uri = Uri::of('https://staging.api.laravel.com');
$this->assertEquals('api.laravel.com', $uri->domain());

$uri = Uri::of('https://test-env.example.co.uk');
$this->assertEquals('example.co.uk', $uri->domain());

$uri = Uri::of('https://example.co.uk');
$this->assertEquals('co.uk', $uri->domain());

$uri = Uri::of('https://hostname');
$this->assertNull($uri->domain());
}

public function test_macroable()
{
Uri::macro('myMacro', function () {
Expand Down