In order to support a consistent and comprehensible API for module developers to create links using the Drupal 8.0.x routing API, code that was forward-ported from Drupal 7 has been retired, the use of route name and parameters required in almost all cases and "system path" is not supported except as a temporary value entered into forms by end users that is converted during save.
This change introduces 2 new static factory methods on the \Drupal\Core\Url
class:
Url::fromRoute($route_name, $route_parameters, $options)
Url::fromUri($uri, $options)
See api page Url::fromUri() for documentation of arguments.
This change also changes \Drupal\Core\Utility\LinkGenerator so that there is a single generate() method that accepts a Url value object, instead of having multiple methods to generate a link.
The renamed _url()
and _l()
are deprecated and *will* be removed before 8.0.0 is released.
Drupal 7:
// Internal path.
$internal_link = l(t('Book admin'), 'admin/structure/book');
// External Uri.
$external_link = l(t('External link'), 'http://www.example.com/', array('external' => TRUE));
Drupal 8:
// Internal path (defined by a route in Drupal 8).
use Drupal\Core\Url;
$url = Url::fromRoute('book.admin');
$internal_link = \Drupal::l(t('Book admin'), $url);
// External Uri.
use Drupal\Core\Url;
$url = Url::fromUri('http://www.example.com/');
$external_link = \Drupal::l(t('External link'), $url);
Update
\Drupal::l
has also been deprecated (as of Drupal 8.0.0). Use \Drupal\Core\Link
instead.
use Drupal\Core\Link;
use Drupal\Core\Url;
$url = Url::fromRoute('book.admin');
$internal_link = Link::fromTextAndUrl(t('Book admin'), $url)->toString();
Similar for the external uri.