这个答案引发了关于如果用户无权访问他们要访问的页面的最佳方式将用户重定向到特定页面的讨论。
一种选择是将访问回调设置为true,然后在页面回调中重定向用户。尽管这似乎是正确的,但我认为它在页面回调中混合了访问功能和页面构建功能。
例如
function hook_menu() {
    $items['player/my_page'] = array(
        'title' => t('My Page'), // note this is a required parameter
        'access callback' => TRUE,
        'page callback' => 'some_function',
    );
    return $items;
}
function some_function() {
    global $user;   
    if(!$user->uid) { // here checking if the user is logged in but could be checking for a specific permission or field value
        $dest = drupal_get_destination();
        drupal_goto('user/login', $dest); // this remembers where the user is coming from
    }
    // carry on building rest of page
}
另一种选择是将访问回调函数设置为调用一个检查用户是否具有访问权限的函数,但是不是返回false而是将用户重定向到另一个页面。这很好,因为它将访问逻辑和页面构建逻辑分开。但是,访问回调的目的是返回一个布尔值,因此这通过重定向用户来破坏该逻辑。
例如
function hook_menu() {
    $items['player/my_page'] = array(
        'title' => t('My Page'), // note this is a required parameter
        'access callback' => 'check_access',
        'page callback' => 'some_function',
    );
    return $items;
}
function check_access() {
    global $user;
    // here checking if the user is logged in but could be checking for a specific permission or field value
    if(!$user->uid) {
        $dest = drupal_get_destination();
        drupal_goto('user/login', $dest);
    }
    return TRUE;
}
在我不知道的访问回调中重定向用户是否会带来不良影响?
您认为这里的最佳做法是什么?
                  在页面回调中的PHP注释中添加一个斜杠:)语法突出显示效果不佳
                
                
                  
                    —
                    xurshid29 
                    
                  
                
              
                  我认为但不是肯定的,如果将此路径添加到菜单中,将会发生一些非常奇怪的事情,访问回调用于确定是否可以为特定用户显示菜单项。
                
                
                  
                    —
                    mpdonadio