How to create a link in drupal 8 ?

Public

Warning message

We recommend you to assign at least one category to this snippet so you could easily navigate it in the future. Edit your snippet here.

How to create a link in drupal 8 ? in d7 we used l function now in d8 we use Url::fromRoute() and Link:fromTextAndUrl() see the following example:

</> CopyGet raw version
php
  1. use Drupal\Core\Link;
  2. use Drupal\Core\Url;
  3.  
  4. $args = ['arg-a' => 'A', 'arg-b' => 'B'];
  5. /* $ drupal console router:debug | search "/user " */
  6. $url = Url::fromRoute('user.page', $args);
  7. $link = Link::fromTextAndUrl('User Profile page', $url)->toString();
  8. // output
  9. <a href="/user?arg-a=A&amp;arg-b=B">Page title</a>