【问题标题】:I am creating a page using hook_menu() but I get "You are not authorized to access this page."我正在使用 hook_menu() 创建一个页面,但我得到“您无权访问此页面”。
【发布时间】:2011-08-24 19:30:52
【问题描述】:

我正在尝试使用hook_menu() 在我的模块中创建一个非常简单的页面,但在我测试它之后,我得到“您无权访问此页面”。我无法弄清楚我做错了什么。以下是我使用的代码。

请注意,我在现有模块包下创建了此模块。例如,模块文件夹是 xyz,我为模块创建了一个文件夹作为 xyz_mobile,我在信息中添加了 xyz 作为包。不知道有没有关系。

function xyz_mobile_menu() {
  $items['mobile'] = array(
    'title' => 'page test',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items; 
}

【问题讨论】:

    标签: drupal drupal-modules drupal-hooks


    【解决方案1】:

    我在这里假设 Drupal 6。您需要 $items 数组中的“访问参数”和“页面回调”元素:

    function mymodule_menu() {
        $items = array();
    
        $items['mobile'] = array(
              'title' => 'page test', 
              'page callback' => 'mymodule_my_function',
              'access callback' => 'user_access',
              'access arguments' => array('access content'), // or another permission
              'type' => MENU_CALLBACK,
        );
    
        return $items;
    }
    

    “访问回调”元素包含函数的名称(在本例中为user_access),该函数将检查用户是否具有“访问参数”元素中指定的权限。

    “页面回调”元素将运行您的自定义函数。

    function mymodule_my_function() {
        return 'this is the test page';
    }
    

    最后,在重新测试之前不要忘记清除菜单缓存。

    【讨论】:

    • 默认情况下,当菜单定义使用“访问参数”属性时,访问回调是“user_access()”。
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多