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

Conversation

stevebauman
Copy link
Contributor

Description

This PR adds two new methods to the Illuminate\Support\Uri class to handle extracting the domain and subdomain components from URIs. This does simple parsing without using any external service, so there are edge cases listed below where the incorrect domain/subdomain will be retrieved.

Usage

domain():

Uri::of('https://api.laravel.com')->domain(); // 'laravel.com'
Uri::of('https://laravel.com')->domain(); // 'laravel.com'

subdomain():

Uri::of('https://api.laravel.com')->subdomain(); // 'api'
Uri::of('https://laravel.com')->subdomain(); // null

Resolved Edge Cases

  • Invalid hostnames return null
  • IP addresses and localhost return null
  • Multi-level subdomains are handled correctly

Unsolved Edge Cases

  • Public suffix domains like user.github.io return user as subdomain
  • Country-code/multipart domains like example.co.uk returns example as subdomain and co.uk as domain

@rodrigopedra
Copy link
Contributor

rodrigopedra commented Jul 17, 2025

Country-code/multipart domains like example.co.uk returns example as subdomain and co.uk as domain

I wouldn't call this an "edge case."

It is a very common case -- actually a day-to-day issue -- for any most projects outside the USA.

Edit: some projects outside the US, use a US-like domain, but still, most doesn't.

@stevebauman
Copy link
Contributor Author

@rodrigopedra That's fine -- whatever you want to call it, doesn't matter to me. If the PR is closed, it's closed. Not a biggie!

@Rizky92
Copy link
Contributor

Rizky92 commented Jul 18, 2025

Would it be more sensible if IP addresses and localhost was returned instead of null? It could be useful for services hosted on local network. Nevermind, you can use ->host() to get them instead.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants