public static Url::fromUserInput($user_input, $options = [])
Creates a Url object for a relative URI reference submitted by user input.
Use this method to create a URL for user-entered paths that may or may not correspond to a valid Drupal route.
string $user_input: User input for a link or path. The first character must be one of the following characters:
This helps reduce ambiguity for user-entered links and paths, and supports user interfaces where users may normally use auto-completion to search for existing resources, but also may type one of these characters to link to (e.g.) a specific path on the site. (With regard to the URI specification, the user input is treated as a relative URI reference where the relative part is of type path-abempty.)
array $options: (optional) An array of options. See Url::fromUri() for details.
static A new Url object based on user input.
\InvalidArgumentException Thrown when the user input does not begin with one of the following characters: '/', '?', or '#'.
public static function fromUserInput($user_input, $options = []) { // Ensuring one of these initial characters also enforces that what is // passed is a relative URI reference rather than an absolute URI, // because these are URI reserved characters that a scheme name may not // start with. if ((strpos($user_input, '/') !== 0) && (strpos($user_input, '#') !== 0) && (strpos($user_input, '?') !== 0)) { throw new \InvalidArgumentException("The user-entered string '$user_input' must begin with a '/', '?', or '#'."); } // fromUri() requires an absolute URI, so prepend the appropriate scheme // name. return static::fromUri('internal:' . $user_input, $options); }
© 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!Url.php/function/Url::fromUserInput/8.1.x