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

StackOverflow https://stackoverflow.com/questions/7181161

Frage

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,
  );

  return $items; 
}
War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top