diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index 04a961c..123cc7a 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -70,7 +70,7 @@ class Url { protected $external = FALSE; /** - * Indicates whether this URL is a URI instead of a route. + * Indicates whether this URL is for a URI without a Drupal route. * * @var bool */ @@ -88,6 +88,10 @@ class Url { /** * Constructs a new Url object. * + * In most cases, use Url::routed() or Url::unrouted rather than constructing + * Url objects directly in order to avoid ambiguity and make your code more + * self-documenting. + * * @param string $route_name * The name of the route * @param array $route_parameters @@ -109,6 +113,12 @@ class Url { * defined, the current scheme is used, so the user stays on HTTP or HTTPS * respectively. if mixed mode sessions are permitted, TRUE enforces HTTPS * and FALSE enforces HTTP. + * + * @see static::routed() + * @see static::unrouted() + * + * @todo Update this documentation for non-routed URIs in + * https://www.drupal.org/node/2346787 */ public function __construct($route_name, $route_parameters = array(), $options = array()) { $this->routeName = $route_name; @@ -117,15 +127,19 @@ public function __construct($route_name, $route_parameters = array(), $options = } /** - * Creates a new routed (internal to Drupal) Url object. + * Creates a new Url object for a URL that has a Drupal route. + * + * This method is for URLs that have Drupal routes (that is, most pages + * generated by Drupal). For non-routed local URIs relative to the base + * path (like robots.txt) use Url::unrouted() with the base:// scheme. * * @param string $route_name * The name of the route * @param array $route_parameters - * (optional) An associative array of parameter names and values. + * (optional) An associative array of route parameter names and values. * @param array $options - * (optional) An associative array of additional options, with the following - * elements: + * (optional) An associative array of additional URL options, with the + * following elements: * - 'query': An array of query key/value-pairs (without any URL-encoding) * to append to the URL. Merged with the parameters array. * - 'fragment': A fragment identifier (named anchor) to append to the URL. @@ -142,22 +156,28 @@ public function __construct($route_name, $route_parameters = array(), $options = * and FALSE enforces HTTP. * * @return \Drupal\Core\Url - * A new routed (internal to Drupal) Url object. + * A new Url object for a routed (internal to Drupal) URL. + * + * @see static::unrouted() */ public static function routed($route_name, $route_parameters = array(), $options = array()) { return new static($route_name, $route_parameters, $options); } /** - * Creates a new unrouted (external to Drupal) Url object. + * Creates a new Url object for a URI that does not have a Drupal route. + * + * This method is for generating URLs for URIs that do not have Drupal + * routes, both external URLs and unrouted local URIs like + * base://robots.txt. For URLs that have Drupal routes (that is, most pages + * generated by Drupal), use Url::routed(). * * @param string $uri * The URI of the external resource including the scheme. For Drupal paths - * that are not handled by the routing system, you may use base:// for the - * scheme. + * that are not handled by the routing system, use base:// for the scheme. * @param array $options - * (optional) An associative array of additional options, with the following - * elements: + * (optional) An associative array of additional URL options, with the + * following elements: * - 'query': An array of query key/value-pairs (without any URL-encoding) * to append to the URL. Merged with the parameters array. * - 'fragment': A fragment identifier (named anchor) to append to the URL. @@ -174,7 +194,9 @@ public static function routed($route_name, $route_parameters = array(), $options * and FALSE enforces HTTP. * * @return \Drupal\Core\Url - * A new unrouted (external to Drupal) Url object. + * A new Url object for an unrouted (non-Drupal) URL. + * + * @see static::routed() */ public static function unrouted($uri, $options = array()) { $url = new static($uri, array(), $options); @@ -221,10 +243,13 @@ public static function createFromRequest(Request $request) { protected function setUnrouted() { $this->unrouted = TRUE; // What was passed in as the route name is actually the URI. + // @todo Consider fixing this in https://www.drupal.org/node/2346787. $this->uri = $this->routeName; // Set empty route name and parameters. $this->routeName = NULL; $this->routeParameters = array(); + // @todo Add a method for the check below in + // https://www.drupal.org/node/2346859. if ($this->external = strpos($this->uri, 'base://') !== 0) { $this->options['external'] = TRUE; } @@ -383,11 +408,11 @@ public function setOption($name, $value) { * A URI not connected to a route. May be an external URL. * * @throws \UnexpectedValueException - * Thrown when the uri was requested for route. + * Thrown when the URI was requested for a routed URL. */ public function getUri() { if (!$this->unrouted) { - throw new \UnexpectedValueException('Internal routes do not have URIs.'); + throw new \UnexpectedValueException('This URL has a Drupal route, so the canonical form is not a URI.'); } return $this->uri;