开发者

I am creating a page using hook_menu() but I get "You are not authorized to access this page."

I am trying to create a very simple page in my module using hook_menu(), but after I test it I get, "You are not authorized to access this page". I can't figure out what I am doing wrong. Following is the code that I used.

Note that I created this module under the existing module package. For instance the module folder is xyz and I created a folder as xyz_mobile for the module, and I added xyz in the info as the package. I don't know if that would have anything to do with it.

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

  return $items; 
}


I'm assuming Drupal 6 here. You need the 'access arguments' and 'page callback' elements in your $items array:

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;
}

The 'access callback' element contains the name of the function (in this case, user_access) that will check if the user has the permission specified in the 'access arguments' element.

The 'page callback' element will run your custom function.

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

Lastly, don't forget to clear the menu cache before you re-test.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜