Skip to main content

[Thủ Thuật Drupal] Drupal 8,9 - Tạo đường dẫn URL với route và tham số trong Twig

Code snippet có thể được sử dụng để tạo đường dẫn URL với route và tham số trong Twig template -  Drupal 8 /Drupal 9.

Relative paths

Sử dụng function  path($name, $parameters, $options)

{#   path= / #}
<a href="{{ path('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a>
{#  path=  /user/1 #}
<a href="{{ path('entity.user.canonical', {'user': 1}) }}">{{ 'View user'|t }}</a>
{#   path /node/1 #}
<a href="{{ path('entity.node.canonical', {'node': 1}) }}">{{ 'View node'|t }}</a>

Absolute  paths

Sử dụng function  url($name, $parameters, $options)

{# return path like this  http://dev.domaine.com/node/1 #}
<a href="{{ url('entity.node.canonical', {'node': 1}) }}">{{ 'View all content'|t }}</a>

{# return path to current page#}
<a href="{{ url('<current>') }}">{{ 'Reload Page'|t }}</a>

{#  return path to home page #}
<a href="{{ url('<front>') }}">{{ 'Home Page'|t }}</a>