How do I get the base path?


You almost never need to explicitly build up paths from parts in Drupal 8. I would go with something like this:

function FOO_preprocess_node(&$variables) {
  /** @var \Drupal\node\NodeInterface $node */
  $node = $variables['node'];
  $variables['url'] = $node->toUrl('canonical', [
    'absolute' => TRUE,
    'language' => $node->language(),

You can use url() to generate a base url to any route in Drupal. You can supply the <front> route which, in effect, will be your base URL.

{{ url('<front>') }}

But, render it first if you're concatenating it:

{{ url('<front>')|render ~ file_url('public://image.jpg') }}

For example:

{{ url('<front>') ~ file_url('public://image.jpg') }}
# Outputs: "Array/sites/all/default/files/image.jpg"

{{ url('<front>')|render ~ file_url('public://image.jpg') }}
# Outputs: ""

In page.html.twig you will have a working {{ base_path }} variable. Why it isn't available in every template, i have no idea. As i think it should be and everything else looks messy, my preferred solution is to do exactly as Drupal does in the preprocess page function for any place i need it.

To make {{ base_path }} available to block templates in a theme named example, add this code to the example.theme file:

 * Implements hook_preprocess_block().
function example_preprocess_block(&$variables) {

  $variables['base_path'] = base_path();

As noted in the accepted answer provided by MPD, if getting a node's or other entity's URL there are methods to do it all for you.

However, there are reasons for getting the base_path, such as when displaying images that live in your theme folder. {{ directory }} provides the path to the theme folder, but it leaves off the base path (usually just a / but in order to preserve Drupal's proper functioning from a subdirectory of a domain that shouldn't be hardcoded). On page.html.twig or in any template which has the above preprocesser, this will work for that purpose:

<img src="{{ base_path ~ directory }}/images/nsf1.svg"
     alt="National Science Foundation logo" height="80" width="80" />

And of course Shawn Conn's answer works fine if you can suppress your desire for pretty templates, but i couldn't.

Tags: Theming

Similar questions

Variable in base path
I need to change the BasePath in drupal 8 of all views and all node in such a way that it contains a variable (which will manage as country) Like this:
php.ini opcache path for multiple websites with shared code base
I have a D9 website set up already and I want to add another that shares the same code base. (This is on a shared hosting environment) Below is an example my current php.ini settings for the opcache with '' as the initial website cache directory path. I want to have the two sites cached in different directories. How do I specify the p...
Drupal6 Deployment: Developed on XAMPP, when online localhost is still the base path
I developed a Drupal site on XAMPP. I deployed it to an Apache webserver by uploading a zip file and .sql file of the database. However, the links on pages generated by Views 2 are still as they were on XAMPP - http://localhost, etc. (So instead of, it's http://localhost). Where do I change this setting? I looked in settings.php but...
Print path to base in template override
I am doing a template override with my template.php, and I need to print the path to the base of my site. Currently I have this: The link created is: I understand why this is happening and why this link doesn't work. If I was adding code to a tpl file, I would use PHP to print the path to the base of my site first. However using the same code in my...
Get path like path/*
I'm writing a module and there is a text area where the users have to add paths; then, I explode these paths, and run some commands. I would like to let users add something like "path/*" (as in context conditions). Is it possible?
How do I get the current path alias or path?
How can I get the current alias and/or path in Drupal 8? In Drupal 7 I did this with drupal_get_path_alias(). What is the equivalent code I should use in Drupal 8?

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.