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
How to get the full URL of a Drupal page?
提问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()
- http://example.com/node/306returns "node/306".
- http://example.com/drupalfolder/node/306returns "node/306" while base_path() returns "/drupalfolder/".
- http://example.com/path/alias(which is a path alias for node/306) returns "node/306" as opposed to the path alias.
- http://example.com/node/306返回“node/306”。
- http://example.com/drupalfolder/node/306返回“node/306”,而 base_path() 返回“/drupalfolder/”。
- http://example.com/path/alias(它是 node/306 的路径别名)返回“node/306”而不是路径别名。
and another function with tiny difference
和另一个细微差别的功能
request_path()
- http://example.com/node/306returns "node/306".
- http://example.com/drupalfolder/node/306returns "node/306" while base_path() returns "/drupalfolder/".
- http://example.com/path/alias(which is a path alias for node/306) returns "path/alias" as opposed to the internal path.
- http://example.com/index.phpreturns an empty string (meaning: front page).
- http://example.com/index.php?page=1returns an empty string.
- http://example.com/node/306返回“node/306”。
- http://example.com/drupalfolder/node/306返回“node/306”,而 base_path() 返回“/drupalfolder/”。
- http://example.com/path/alias(它是 node/306 的路径别名)返回“路径/别名”而不是内部路径。
- http://example.com/index.php返回一个空字符串(意思是:首页)。
- http://example.com/index.php?page=1返回一个空字符串。
回答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();

