如何在Drupal中获取当前页面URL?

问题描述 投票:0回答:5

我正在尝试在 Drupal 站点(Drupal 7)上输出当前页面 URL,我尝试了以下操作...

<?php print current_path(); ?>

它输出页面名称而不是 URL,关于如何输出完整 URL 的任何想法吗?

php drupal drupal-7 drupal-theming drupal-9
5个回答
2
投票

在直接 PHP 中你可以使用这个:

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

我不明白为什么这行不通。这将为您提供路径和 URL。


2
投票

你可以使用

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

1
投票

如果您想以“drupal 方式”完成工作,可以使用 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


1
投票

在 drupal9 中

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

0
投票

在D7上如果你想获取当前路径可以使用:

$path = drupal_get_path_alias();

因此,如果您当前的 URL 是:

www.test.com/hello/world

您将得到:

“你好/世界”

© www.soinside.com 2019 - 2024. All rights reserved.