[英]How to get current page URL in Drupal?
I'm trying to output the current page URL on a Drupal site (Drupal 7), I've tried the following...我正在尝试在 Drupal 站点(Drupal 7)上输出当前页面 URL,我尝试了以下操作...
<?php print current_path(); ?>
Which outputs the page name but not the URL, any ideas on how to output the full URL too?哪个输出页面名称而不是 URL,关于如何输出完整 URL 的任何想法?
In straight PHP you can use this: 在直接的PHP中,您可以使用以下代码:
$curPathName = $_SERVER["DOCUMENT_ROOT"];
$currentURL = $_SERVER["HTTP_HOST"];
I don't see why that wouldn't work. 我不明白为什么这行不通。 That will give you the path and the URL.
这将为您提供路径和URL。
If you want to keep things done "the drupal way", you can use current_path() or request_path(). 如果您想以“简化的方式”完成工作,则可以使用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 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
You can also use $base_url instead of relying on $_SERVER. 您也可以使用$ base_url而不是依赖$ _SERVER。
The solution durbpoisn gave will give you the URL in the browser. durbpoisn提供的解决方案将为您提供浏览器中的URL。 If that is an alias, you can use drupal_get_normal_path() to get the internal path
如果这是别名,则可以使用drupal_get_normal_path()获取内部路径。
https://api.drupal.org/api/drupal/includes!path.inc/function/drupal_get_normal_path/7 https://api.drupal.org/api/drupal/includes!path.inc/function/drupal_get_normal_path/7
You can use 您可以使用
<?php
global $base_url;
print $base_url . '/' . current_path();
?>
On D7 if you want to get the current path you can use:在 D7 上,如果您想获取当前路径,可以使用:
$path = drupal_get_path_alias();
So if your current URL is:因此,如果您当前的网址是:
www.test.com/hello/world www.test.com/hello/world
You will get:你会得到:
'hello/world' '你好,世界'
in drupal9在 drupal9
$current_path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.