W3cubDocs

/Drupal 8

public static function DrupalKernel::findSitePath

public static DrupalKernel::findSitePath(Request $request, $require_settings = TRUE)

Returns the appropriate site directory for a request.

Once the kernel has been created DrupalKernelInterface::getSitePath() is preferred since it gets the statically cached result of this method.

Site directories contain all site specific code. This includes settings.php for bootstrap level configuration, file configuration stores, public file storage and site specific modules and themes.

Finds a matching site directory file by stripping the website's hostname from left to right and pathname from right to left. By default, the directory must contain a 'settings.php' file for it to match. If the parameter $require_settings is set to FALSE, then a directory without a 'settings.php' file will match as well. The first configuration file found will be used and the remaining ones will be ignored. If no configuration file is found, returns a default value 'sites/default'. See default.settings.php for examples on how the URL is converted to a directory.

If a file named sites.php is present in the sites directory, it will be loaded prior to scanning for directories. That file can define aliases in an associative array named $sites. The array is written in the format '<port>.<domain>.<path>' => 'directory'. As an example, to create a directory alias for https://www.drupal.org:8080/mysite/test whose configuration file is in sites/example.com, the array should be defined as:

$sites = array(
  '8080.www.drupal.org.mysite.test' => 'example.com',
);

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

bool $require_settings: Only directories with an existing settings.php file will be recognized. Defaults to TRUE. During initial installation, this is set to FALSE so that Drupal can detect a matching directory, then create a new settings.php file in it.

Return value

string The path of the matching directory.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException In case the host name in the request is invalid.

See also

\Drupal\Core\DrupalKernelInterface::getSitePath()

\Drupal\Core\DrupalKernelInterface::setSitePath()

default.settings.php

example.sites.php

File

core/lib/Drupal/Core/DrupalKernel.php, line 335

Class

DrupalKernel
The DrupalKernel class is the core of Drupal itself.

Namespace

Drupal\Core

Code

public static function findSitePath(Request $request, $require_settings = TRUE) {
  if (static::validateHostname($request) === FALSE) {
    throw new BadRequestHttpException();
  }

  // Check for a simpletest override.
  if ($test_prefix = drupal_valid_test_ua()) {
    return 'sites/simpletest/' . substr($test_prefix, 10);
  }

  // Determine whether multi-site functionality is enabled.
  if (!file_exists(DRUPAL_ROOT . '/sites/sites.php')) {
    return 'sites/default';
  }

  // Otherwise, use find the site path using the request.
  $script_name = $request->server->get('SCRIPT_NAME');
  if (!$script_name) {
    $script_name = $request->server->get('SCRIPT_FILENAME');
  }
  $http_host = $request->getHttpHost();

  $sites = array();
  include DRUPAL_ROOT . '/sites/sites.php';

  $uri = explode('/', $script_name);
  $server = explode('.', implode('.', array_reverse(explode(':', rtrim($http_host, '.')))));
  for ($i = count($uri) - 1; $i > 0; $i--) {
    for ($j = count($server); $j > 0; $j--) {
      $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
      if (isset($sites[$dir]) && file_exists(DRUPAL_ROOT . '/sites/' . $sites[$dir])) {
        $dir = $sites[$dir];
      }
      if (file_exists(DRUPAL_ROOT . '/sites/' . $dir . '/settings.php') || (!$require_settings && file_exists(DRUPAL_ROOT . '/sites/' . $dir))) {
        return "sites/$dir";
      }
    }
  }
  return 'sites/default';
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!DrupalKernel.php/function/DrupalKernel::findSitePath/8.1.x