51

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??

Shane
  • 2,375
  • 4
  • 22
  • 31

7 Answers7

77

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'].

lowerends
  • 5,469
  • 2
  • 24
  • 36
  • I do not know what the issue was. Now it seems to be working fine. I testing it for an hour. The only thing I can think of was the vhost in apache on my localhost was not correct. I moved the public folder and did not update this. This may have effected it. But what I described was defiantly happening. I kept returning the URL of the current page. I was going crazy... – Shane Jul 28 '14 at 14:06
  • 1
    $_SERVER['SERVER_NAME'] returns the name of the server host: "The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host." This will not always be the url the user is on, e.g. if you have aliases in your vhosts. Request::root(); works fine in this occassion. – Pim Oct 21 '14 at 09:33
  • 4
    Better to use a regex because if you ever switch to `https://` then your code is broken. – Jake Wilson Jan 03 '15 at 21:04
  • If you're looking to parse a url I'd recommend `parse_url`. – Mr_Chimp Jul 03 '17 at 13:51
  • 1
    This code is broken if used with any other protocol that doesn't have exactly 4 characters – Emil Rosenius Sep 07 '17 at 12:02
  • I don't think this should be the accepted answer. There is a comment by lowerends on the original question that is much cleaner – Francisco Ochoa Jul 10 '18 at 21:00
  • getHost is not a static method. instead use `response()->getHost()` or `$request->getHost()` – Rejaul Nov 25 '20 at 08:34
  • You cant use this anymore. "should not be called statically" error will show – tmarois Jun 12 '22 at 19:57
36

You also may test any of these:

Request::server ("SERVER_NAME")
Request::server ("HTTP_HOST")

It seems better than making any treatment of

Request::root()

All right.

Seiji Manoan
  • 671
  • 6
  • 12
36

In Laravel 5.1 and later you can use

request()->getHost();

or

request()->getHttpHost();

(the second one will add port if it's not standard one)

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
19

My hint:

  1. FIND IF EXISTS in .env:

    APP_URL=http://yourhost.dev

  2. REPLACE TO (OR ADD)

    APP_DOMAIN=yourhost.dev

  3. FIND in config/app.php:

    'url' => env('APP_URL'),

  4. REPLACE TO

    'domain' => env('APP_DOMAIN'),

    'url' => 'http://' . env('APP_DOMAIN'),

  5. USE:

    Config::get('app.domain'); // yourhost.dev

    Config::get('app.url') // http://yourhost.dev

  6. Do your magic!

kivagant
  • 1,849
  • 2
  • 24
  • 33
  • 2
    P.S.: You free to add any environment variables and use it this way, for example: for api domain, for account domain, for help documentation domain and any others. But, you don't really need to add development domains in .env (like APP_DEV_DOMAIN). Just use another environment (develop.env, behat.env, qa.env etc) with another values. (sorry for ruenglish) – kivagant May 22 '15 at 17:43
10

This is for Laravel 5.1 and I am not sure does it work for earlier versions but if somebody search on Google and lands here it might be handy in middleware handle function gets $request parameter:

$request->server->get('SERVER_NAME')

outside of middleware handle method you can access it by helper function request()

request()->server->get('SERVER_NAME')
Zoran
  • 2,653
  • 2
  • 13
  • 9
1

use directly where you want controller or web.php

Request::getHost();
Balaji
  • 9,657
  • 5
  • 47
  • 47
  • double check your code above getHost() not using $this object ,you may used $this invalid place like https://stackoverflow.com/a/14154804/11624647 – Balaji Feb 15 '22 at 18:13
-19

I think you can use asset('/')

maspai
  • 397
  • 6
  • 17