public static function Database::getConnectionInfoAsUrl

public static Database::getConnectionInfoAsUrl($key = 'default')

Gets database connection info as a URL.

Parameters

string $key: (Optional) The database connection key.

Return value

string The connection info as a URL.

File

core/lib/Drupal/Core/Database/Database.php, line 499

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static function getConnectionInfoAsUrl($key = 'default') {
  $db_info = static::getConnectionInfo($key);
  if ($db_info['default']['driver'] == 'sqlite') {
    $db_url = 'sqlite://localhost/' . $db_info['default']['database'];
  }
  else {
    $user = '';
    if ($db_info['default']['username']) {
      $user = $db_info['default']['username'];
      if ($db_info['default']['password']) {
        $user .= ':' . $db_info['default']['password'];
      }
      $user .= '@';
    }

    $db_url = $db_info['default']['driver'] . '://' . $user . $db_info['default']['host'];
    if (isset($db_info['default']['port'])) {
      $db_url .= ':' . $db_info['default']['port'];
    }
    $db_url .= '/' . $db_info['default']['database'];
  }
  if ($db_info['default']['prefix']['default']) {
    $db_url .= '#' . $db_info['default']['prefix']['default'];
  }
  return $db_url;
}

© 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!Database!Database.php/function/Database::getConnectionInfoAsUrl/8.1.x