简体   繁体   English

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

[英]How to get the full URL of a Drupal page?

I need to be grabbing the URL of the current page in a Drupal site.我需要在 Drupal 站点中获取当前页面的 URL。 It doesn't matter what content type it is - can be any type of node.它是什么内容类型并不重要——可以是任何类型的节点。

I am NOT looking for the path to theme, or the base url, or Drupal's get_destination.我不是在寻找主题的路径,或者基础 url,或者 Drupal 的 get_destination。 I'm looking for a function or variable that will give me the following in full:我正在寻找一个 function 或变量,它将为我提供以下完整信息:

http://example.com/node/number http://example.com/node/number

Either with or without (more likely) the http:// .有或没有(更有可能) http://

drupal_get_destination() has some internal code that points at the correct place to getthe current internal path. drupal_get_destination()有一些内部代码指向正确的位置以获取当前内部路径。 To translate that path into an absolute URL, the url() function should do the trick.要将该路径转换为绝对的 URL, url() function 应该可以解决问题。 If the 'absolute' option is passed in it will generate the full URL, not just the internal path.如果传入“绝对”选项,它将生成完整的 URL,而不仅仅是内部路径。 It will also swap in any path aliases for the current path as well.它还将交换当前路径的任何路径别名。

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

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()

You can also do it this way:你也可以这样做:

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

It's a bit faster.它有点快。

Try the following:尝试以下操作:

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

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和另一个 function 差别很小

request_path()

I find using tokens pretty clean.我发现使用令牌非常干净。 It is integrated into core in Drupal 7.它集成到 Drupal 7 的内核中。

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

The following is more Drupal-ish :以下是更多Drupal 风格

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

For Drupal 8 you can do this:对于 Drupal 8 ,您可以这样做:

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

Maybe what you want is just plain old predefined variables.也许您想要的只是普通的旧预定义变量。

Consider trying考虑尝试

$_SERVER['REQUEST_URI'']

Or read more here .或者在这里阅读更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM