【问题标题】:How to get current page URL in Drupal?如何在 Drupal 中获取当前页面 URL?
【发布时间】:2015-12-23 15:32:19
【问题描述】:

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

<?php print current_path(); ?>

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

【问题讨论】:

    标签: php drupal drupal-7 drupal-theming drupal-9


    【解决方案1】:

    在 PHP 中你可以使用这个:

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

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

    【讨论】:

    • 我选择了 这似乎有效。还是谢谢。
    • 好吧,那你可以接受答案,因为这是我的建议。 :)
    【解决方案2】:

    你可以使用

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

    【讨论】:

      【解决方案3】:

      如果你想以“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

      【讨论】:

        【解决方案4】:

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

        $path = drupal_get_path_alias();
        

        所以如果你当前的网址是:

        www.test.com/hello/world

        你会得到:

        '你好/世界'

        【讨论】:

          【解决方案5】:

          在 Drupal9 中

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-22
            • 2011-07-15
            • 2017-08-23
            • 2021-06-07
            • 2011-06-17
            相关资源
            最近更新 更多