How to get the base URL of a site

Solutions

There are some warnings about directly accessing the request object in this way in \Drupal::request:

 * Note: The use of this wrapper in particular is especially discouraged. Most
 * code should not need to access the request directly.  Doing so means it
 * will only function when handling an HTTP request, and will require special
 * modification or wrapping when run from a command line tool, from certain
 * queue processors, or from automated tests.
 *
 * If code must access the request, it is considerably better to register
 * an object with the Service Container and give it a setRequest() method
 * that is configured to run when the service is created.  That way, the
 * correct request object can always be provided by the container and the
 * service can still be unit tested.

Any form controller extending \Drupal\Core\Form\FormBase automatically has this dependency injected, and it may be accessed using:

$this->getRequest()->getSchemeAndHttpHost()

I think (but haven't tested) that a regular page controller extending \Drupal\Core\Controller\ControllerBase could provide the request_stack service by overriding the \Drupal\Core\Controller\ControllerBase::create function, and then setting a $request property in the constructor. This is described really well for forms, and the same process should apply for page controllers: https://www.drupal.org/docs/8/api/services-and-dependency-injection/dependency-injection-for-a-form.

You can get the hostname, "drupal8.local", directly from the getHost() request:

$host = \Drupal::request()->getHost();

In some cases you might want to get the schema as well, fx https://drupal8.local:

$host = \Drupal::request()->getSchemeAndHttpHost();

If you'd like to do this with dependency injection and a service, then you can use RequestStack:

use Symfony\Component\HttpFoundation\RequestStack;

And define it like this:

protected $request;

public function __construct(..., RequestStack $request_stack) {
  ...
  $this->request = $request_stack->getCurrentRequest();
}

public static function create(ContainerInterface $container, ...) {
  return new static(
    ...
    $container->get('request_stack')
  )
}

And then declare it like this:

$this->request->getHost()
$this->request->getSchemeAndHttpHost()
Tags: Drupal 8 / Uri

Similar questions

How to get the base url?
(Drupal 7.22) We need to create a menu link to an external site, "https://example.com/some/path" /some/path is static, whereas "https://example.com" depends on site instance, such as web-dev.example.com web-qa.exmple.com web-rc.example.com I would think that this could be accomplished with $GLOBALS['base_root']; for example: $url = $GLOBALS['base_r...
Uri
How do I get the base URL from the feed content?
Basically, I'm using the Aggregator module to import content from an RSS feed. The content is just a bunch of articles. I display this content in a view. This is my current output, it shows the title and the date in a jcarousel. The thing I'm missing is the webpage source. Currently, source url in views will have me a full link like: https://whatiw...
Base url issues and subdirectories
I have a drupal site at http://example.com and a development version of the same site at http://another.example.com/subdir. When I'm working on the development version, content that references /sites/default/files/ - such as images - doesn't work because it needs to include the '/subdir' part of the path. Setting $base_url doesn't work because that...
Uri
Set a views path to base URL
I am using view. Every view needs path and i want to set that path to my base url. For example i want to make a view which will take a Car as a argument and show result at Any help will be appreciated.
How to change my base URL
I have built a website using drupal. Now, the base URL is http://xxx.xxx.xxx.xxx/drupal. But I want to change base URL as http://xxx.xxx.xxx.xxx How do I do? Thanks.
Uri
AJAX calls fail when the base URL is set
I have installed the Drupal 7.17 security update, and I am having some issues with my site. If I comment out the $base_url ='www.xxx.com'; line in the setting.php file, the AJAX calls work; when I uncomment that line, the AJAX calls fail. What could be the reason behind this? how can I get my base_url working again?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.