How to get the full URL of a Drupal page?

2019-01-30 10:29发布

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.

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:

http://example.com/node/number

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

9条回答
走好不送
2楼-- · 2019-01-30 11:08

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

查看更多
beautiful°
3楼-- · 2019-01-30 11:09

You can also do it this way:

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

It's a bit faster.

查看更多
贼婆χ
4楼-- · 2019-01-30 11:15

Try the following:

url($_GET['q'], array('absolute' => true));
查看更多
Bombasti
5楼-- · 2019-01-30 11:15

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

    current_path()

and another function with tiny difference

request_path()
查看更多
forever°为你锁心
6楼-- · 2019-01-30 11:16

Maybe what you want is just plain old predefined variables.

Consider trying

$_SERVER['REQUEST_URI'']

Or read more here.

查看更多
仙女界的扛把子
7楼-- · 2019-01-30 11:21

For Drupal 8 you can do this :

$url = 'YOUR_URL';
$url = \Drupal\Core\Url::fromUserInput('/' . $url,  array('absolute' => 'true'))->toString();
查看更多
登录 后发表回答