- // 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);
- ==================================or==================================================
- use Drupal\Core\Url;
- use Drupal\Core\Link;
- $url = Url::fromRoute('entity.node.edit_form', array('node' => NID));
- $project_link = Link::fromTextAndUrl(t('Open Project'), $url);
- $project_link = $project_link->toRenderable();
- // If you need some attributes.
- $project_link['#attributes'] = array('class' => array('button', 'button-action', 'button--primary', 'button--small'));
- print render($project_link);
https://chromatichq.com/blog/creating-links-within-twig-templates-using-path-and-url
// Link to the default frontpage content listing view:
<a href="{{ path('view.frontpage') }}">{{ 'View all content'|t }}</a>
// Link to a specific node page:
<a href="{{ path('entity.node.canonical', {'node': node.id}) }}">{{ 'Read more'|t }}</a>
// Link to a specific user profile page:
<a href="{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a>
// Link to a view, and throw in some additional query string parameters:
<a href="{{ path('view.articles.page_1', {'page': 2}) }}">{{ 'Go to page 2'|t }}</a>
// Link to a view and pass in some arguments to the view:
<a href="{{ path('view.recent_articles_by_author.page_1', {'arg_0': user.field_display_name.value|drupal_escape }) }}">{{ 'See all the articles written by'|t }} {{ user.field_display_name.value }}</a>