簡體   English   中英

如何在 Drupal 中獲取當前頁面 URL?

[英]How to get current page URL in Drupal?

我正在嘗試在 Drupal 站點(Drupal 7)上輸出當前頁面 URL,我嘗試了以下操作...

<?php print current_path(); ?>

哪個輸出頁面名稱而不是 URL,關於如何輸出完整 URL 的任何想法?

在直接的PHP中,您可以使用以下代碼:

$curPathName = $_SERVER["DOCUMENT_ROOT"];
$currentURL = $_SERVER["HTTP_HOST"];

我不明白為什么這行不通。 這將為您提供路徑和URL。

如果您想以“簡化的方式”完成工作,則可以使用current_path()或request_path()。

https://api.drupal.org/api/drupal/includes!path.inc/function/current_path/7 https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/request_path/7

您也可以使用$ base_url而不是依賴$ _SERVER。

durbpoisn提供的解決方案將為您提供瀏覽器中的URL。 如果這是別名,則可以使用drupal_get_normal_path()獲取內部路徑。

https://api.drupal.org/api/drupal/includes!path.inc/function/drupal_get_normal_path/7

您可以使用

<?php 
    global $base_url;
    print $base_url . '/' . current_path(); 
?>

在 D7 上,如果您想獲取當前路徑,可以使用:

$path = drupal_get_path_alias();

因此,如果您當前的網址是:

www.test.com/hello/world

你會得到:

'你好,世界'

在 drupal9

$current_path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM