php 如何获取 Drupal 页面的完整 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/703426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:34:24  来源:igfitidea点击:

How to get the full URL of a Drupal page?

phpdrupalcontent-management-system

提问by Eaton

I need to be grabbing the URL of the current page in a Drupal site. It doesn't matter what content type it is - can be any type of node.

我需要在 Drupal 站点中获取当前页面的 URL。它是什么内容类型无关紧要 - 可以是任何类型的节点。

I am NOT looking for the path to theme, or the base url, or Drupal's get_destination. I'm looking for a function or variable that will give me the following in full:

我不是在寻找主题的路径、基本 url 或 Drupal 的 get_destination。我正在寻找一个函数或变量,它会给我完整的以下内容:

http://example.com/node/number

http://example.com/node/number

Either with or without (more likely) the http://.

有或没有(更有可能)http://.

回答by Eaton

drupal_get_destination()has some internal code that points at the correct place to getthe current internal path. To translate that path into an absolute URL, the url()function should do the trick. If the 'absolute' option is passed in it will generate the full URL, not just the internal path. It will also swap in any path aliases for the current path as well.

drupal_get_destination()有一些内部代码指向正确的位置来获取当前的内部路径。要将路径转换为绝对 URL,url()函数应该可以解决问题。如果传入 'absolute' 选项,它将生成完整的 URL,而不仅仅是内部路径。它还将为当前路径交换任何路径别名。

$path = isset($_GET['q']) ? $_GET['q'] : '<front>';
$link = url($path, array('absolute' => TRUE));

回答by mikeytown2

This is what I found to be useful

这是我发现有用的

global $base_root;
$base_root . request_uri();

Returns query strings and it's what's used in core: page_set_cache()

返回查询字符串,它是核心使用的内容:page_set_cache()

回答by ThomasR

You can also do it this way:

你也可以这样做:

$current_url = 'http://' .$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

It's a bit faster.

它有点快。

回答by ax.

Try the following:

请尝试以下操作:

url($_GET['q'], array('absolute' => true));

回答by Yuseferi

This method all is old method, in drupal 7 we can get it very simple

这个方法都是老方法,在drupal 7中我们可以很简单

    current_path()

and another function with tiny difference

和另一个细微差别的功能

request_path()

回答by Yo-L

I find using tokens pretty clean. It is integrated into core in Drupal 7.

我发现使用令牌非常干净。它被集成到 Drupal 7 的核心中。

<?php print token_replace('[current-page:url]'); ?>

回答by kenorb

The following is more Drupal-ish:

以下是更多Drupal 风格的

url(current_path(), array('absolute' => true)); 

回答by Jitendra Mittal

For Drupal 8you can do this :

对于 Drupal 8,您可以这样做:

$url = 'YOUR_URL';
$url = \Drupal\Core\Url::fromUserInput('/' . $url,  array('absolute' => 'true'))->toString();

回答by Sergej Andrejev

Maybe what you want is just plain old predefined variables.

也许您想要的只是普通的旧预定义变量。

Consider trying

考虑尝试

$_SERVER['REQUEST_URI'']

Or read more here.

在此处阅读更多内容。