我第一次尝试使用规则模块,我试图用一些简单的 php 代码重定向我的用户,如下所示:
drupal_set_message('testing');
drupal_goto('node/3');
第一行代码执行,但第二行应该将我的用户引导到 node/3,但没有达到预期的效果。
如何使此重定向功能正常工作?
我第一次尝试使用规则模块,我试图用一些简单的 php 代码重定向我的用户,如下所示:
drupal_set_message('testing');
drupal_goto('node/3');
第一行代码执行,但第二行应该将我的用户引导到 node/3,但没有达到预期的效果。
如何使此重定向功能正常工作?
这很可能是因为您?destination=some/path
在页面 URL 中有这些行,这些行drupal_goto()
会导致您传递给函数的任何路径都被 URL 中的任何内容覆盖:
if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
$destination = drupal_parse_url($_GET['destination']);
$path = $destination['path'];
// ...
您可能只需将代码更改为以下内容即可绕过它:
if (isset($_GET['destination'])) {
unset($_GET['destination']);
}
drupal_goto('node/3');
如果这不起作用,请尝试在之前添加此行drupal_goto
:
drupal_static_reset('drupal_get_destination');
这将重置drupal_get_destination()
函数的静态缓存,它也会在某些时候参与到这个过程中(我认为)。
如果一切都失败了,去老学校:
$path = 'node/3';
$options = array('absolute' => TRUE);
$url = url($path, $options);
$http_response_code = 302;
header('Location: ' . $url, TRUE, $http_response_code);
drupal_exit($url);
这几乎是直接从drupal_goto()
函数本身中删除的,并且肯定会起作用。