2
function eventi_menu() {
  $items = array();
  $items['eventi/book/%/'] = array(
    'title' => 'Book',
    'page callback' => 'eventi_book',
    'access arguments' => array('allow booking'),
    'page arguments' => array(2),
    'type' => MENU_CALLBACK
  );
  $items['eventi/book_waits/%/'] = array(
    'title' => 'Book Waits',
    'page callback' => 'eventi_book',
    'access arguments' => array('allow booking'),
    'page arguments' => array(2),
    'type' => MENU_CALLBACK
  );
  $items['eventi/unbook/%/'] = array(
    'title' => 'Unbook',
    'page callback' => 'eventi_unbook',
    'access arguments' => array('allow booking'),
    'page arguments' => array(2),
    'type' => MENU_CALLBACK
  );

  return $items;
}

function eventi_book($nid) {
  echo 'something';
}

All worked OK, but suddenly, when I visit (for example) http://example.com/eventi/book/201, I get an error 404. I cleared the cache, but I keep to get the same error. Do you have any clue why it happens?

1

Remove the ending slash in the paths.

function eventi_menu() {
  $items = array();

  $items['eventi/book/%'] = array(
    'title' => 'Book',
    'page callback' => 'eventi_book',
    'access arguments' => array('allow booking'),
    'page arguments' => array(2),
    'type' => MENU_CALLBACK
  );
  $items['eventi/book_waits/%'] = array(
    'title' => 'Book Waits',
    'page callback' => 'eventi_book',
    'access arguments' => array('allow booking'),
    'page arguments' => array(2),
    'type' => MENU_CALLBACK
  );
  $items['eventi/unbook/%'] = array(
    'title' => 'Unbook',
    'page callback' => 'eventi_unbook',
    'access arguments' => array('allow booking'),
    'page arguments' => array(2),
    'type' => MENU_CALLBACK
  );

  return $items;
}
4

I would guess that trailing slash on the path is throwing you

$items['eventi/book/%'] = array(
  'title' => 'Book',
  'page callback' => 'eventi_book',
  'access arguments' => array('allow booking'),
  'page arguments' => array(2),
  'type' => MENU_CALLBACK
);

Also, if you use %node for the variable in the path, the menu router will call node_load() on the given nid from the path and you'll get the full $node object in your callback function, like so:

$items['eventi/book/%node'] = array(
  'title' => 'Book',
  'page callback' => 'eventi_book',
  'access arguments' => array('allow booking'),
  'page arguments' => array(2),
  'type' => MENU_CALLBACK
);

// ...and so on, until...

function eventi_book($node) {
   return $node->title;
}

Also note that your callback function should return a value, not echo it.

1
  • 1
    A menu callback can print a value: see what is done from node_feed(), which is the menu callback for the path rss.xml, and which prints its output. The difference between printing the result, and returning a string is that in the latter case index.php passes the string, as $return, to theme('page', $return). Another difference is that when the menu callback returns a string, that string is cached by Drupal for anonymous users. – kiamlaluno Apr 27 '11 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.