Generates an internal or external URL.
When creating links in modules, consider whether l() could be a better alternative than url().
\Drupal\Core\Routing\PathBasedGeneratorInterface::generateFromPath().
function url($path = NULL, array $options = array()) {
$generator = Drupal::urlGenerator();
try {
$url = $generator
->generateFromPath($path, $options);
} catch (GeneratorNotInitializedException $e) {
// Fallback to using globals.
// @todo Remove this once there is no code that calls url() when there is
// no request.
global $base_url, $base_path, $script_path;
$generator
->setBasePath($base_path);
$generator
->setBaseUrl($base_url . '/');
$generator
->setScriptPath($script_path);
$url = $generator
->generateFromPath($path, $options);
}
return $url;
}