public static DrupalKernel::findSitePath(Request $request, $require_settings = TRUE,$app_root= NULL)
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.
string $app_root: (optional) The path to the application root as a string. If not supplied, the application root will be computed.
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 359
Class
- DrupalKernel
- The DrupalKernel class is the core of Drupal itself.
Namespace
Drupal\Core
Code
public static function findSitePath(Request $request, $require_settings = TRUE, $app_root = NULL) { if (static::validateHostname($request) === FALSE) { throw new BadRequestHttpException(); } if ($app_root === NULL) { $app_root = static::guessApplicationRoot(); } // Check for a simpletest override. if ($test_prefix = drupal_valid_test_ua()) { $test_db = new TestDatabase($test_prefix); return $test_db->getTestSitePath(); } // Determine whether multi-site functionality is enabled. if (!file_exists($app_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 $app_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($app_root . '/sites/' . $sites[$dir])) { $dir = $sites[$dir]; } if (file_exists($app_root . '/sites/' . $dir . '/settings.php') || (!$require_settings && file_exists($app_root . '/sites/' . $dir))) { return "sites/$dir"; } } } return 'sites/default'; }
Please login to continue.