Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm ready to scream how hard can this be? I've been trying for too long. If I have http://www.example.com/more/pages/page.php or similar I want to be able to get www.example.com.

Thats all. So I can use it as I please. This will of course change if on production or development so I want to ascertain it dynamically.

Request::root()

returns http://www.example.com/more/pages/page.php

URL::to('/')

returns http://www.example.com/more/pages/page.php

How do I get this? Why am I having so much trouble to do this??

question from:https://stackoverflow.com/questions/24996163/how-to-get-domain-root-url-in-laravel-4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
72 views
Welcome To Ask or Share your Answers For Others

1 Answer

UPDATE (2017-07-12)

A better solution is actually to use Request::getHost()

Previous answer:

I just checked and Request::root(); does return http://www.example.com in my case, no matter which route I'm on. You can then do the following to strip off the http:// part:

if (starts_with(Request::root(), 'http://'))
{
    $domain = substr (Request::root(), 7); // $domain is now 'www.example.com'
}

You may want to double check or post more code (routes.php, controller code, ...) if the problem persists.

Another solution is to simply use $_SERVER['SERVER_NAME'].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...