use Drupal\Core\Url; // Define the path and options for the URL $path = '/node/123'; $options = [ 'absolute' => true, // generate an absolute URL 'query' => ['param1' => 'value1'], // add a query string to the URL 'fragment' => 'section1', // add a fragment identifier to the URL ]; // Create the formatted URL $url = Url::fromUri($path, $options)->toString();
use Drupal\Core\Url; // Define the URL string to parse $url_string = 'https://www.example.com/path/to/page?query1=value1&query2=value2#section1'; // Parse the URL into its components $url = Url::fromUri($url_string); // Get the different parts of the URL $scheme = $url->getScheme(); // "https" $host = $url->getHost(); // "www.example.com" $path = $url->getPath(); // "/path/to/page" $query = $url->getQuery(); // "query1=value1&query2=value2" $fragment = $url->getFragment(); // "section1"In summary, the Drupal core Url package library is a useful tool for managing URLs within a Drupal application. It provides various functions to format, parse, and generate URLs, among other capabilities. Developers can use this library to create SEO-friendly URLs, handle different types of URL requests, and improve the user experience of their Drupal sites.