//Get Path/Route from Url Object $url = Url::fromRoute('entity.node.canonical', ['node' => 1], []);
if ($url->isRouted()) {
$out = $url->toString(); // Get : /en/node/1
$out = $url->getInternalPath(); // Get : node/1
$out = $url->getRouteName(); // Get : entity.node.canonical
$out = $url->getRouteParameters(); // Get : Array ([node] => 1)
$out = $url->getOptions(); // Get : Array ()
}
Create a route from external Uri
Get Data from External Url Object $url = Url::fromUri("http://www.drupal8.ovh/en/tutoriels/275/drupal-8-url-tips-get-name-path-args?test=yes");
if ($url->isExternal()) {
$out = $url->toString();//Get : http://www.drupal8.ovh/en/tutoriels/275/drupal-8-url-tips-get-name-path-args?test=yes
$out = $url->getOptions(); // Get : Array([external] => 1)
$out = $url->getUri(); //Get : http://www.drupal8.ovh/en/tutoriels/275/drupal-8-url-tips-get-name-path-args?test=yes
}
Note : If you are not sure, its better to use try ant chtch to avoid exceptions. try {
$url->getUri();
}
catch (\Exception $e) {
drupal_set_message("Has internam path : " . $url->toString());
}
Comments