page_example_menu

Versions
4.6 – 5
page_example_menu($may_cache)
6 – 7
page_example_menu()

Implementation of hook_menu().

You must implement hook_menu() to emit items to place in the main menu. This is a required step for modules wishing to display their own pages, because the process of creating the links also tells Drupal what callback function to use for a given URL. The menu items returned here provide this information to the menu system.

With the below menu definitions, URLs will be interpreted as follows:

If the user accesses http://example.com/?q=foo, then the menu system will first look for a menu item with that path. In this case it will find a match, and execute page_example_foo().

If the user accesses http://example.com/?q=bar, no match will be found, and a 404 page will be displayed.

If the user accesses http://example.com/?q=bar/baz, the menu system will find a match and execute page_example_baz().

If the user accesses http://example.com/?q=bar/baz/1/2, the menu system will first look for bar/baz/1/2. Not finding a match, it will look for bar/baz/1/%. Again not finding a match, it will look for bar/baz/%/2. Yet again not finding a match, it will look for bar/baz/%/%. This time it finds a match, and so will execute page_example_baz(1, 2). Note the parameters being passed; this is a very useful technique.

Code

developer/examples/page_example.module, line 65

<?php
function page_example_menu() {
  // This is the minimum information you can provide for a menu item.
  $items['foo'] = array(
    'title' => 'Foo',
    'page callback' => 'page_example_foo',
    'access arguments' => array('access foo'),
  );

  // By using the MENU_CALLBACK type, we can register the callback for this
  // path but not have the item show up in the menu; the admin is not allowed
  // to enable the item in the menu, either.
  //
  // Notice that the 'page arguments' is an array of numbers. These will be
  // replaced with the corresponding parts of the menu path. In this case a 0
  // would be replaced by 'bar', a 1 by 'baz', and like wise 2 and 3 will be
  // replaced by what ever the user provides. These will be passed as arguments
  // to the page_example_baz() function.
  $items['bar/baz/%/%'] = array(
    'title' => 'Baz',
    'page callback' => 'page_example_baz',
    'page arguments' => array(2, 3),
    'access arguments' => array('access baz'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}
?>

bar/baz won't work

Jax - Thu, 2009-10-22 08:19

The example states that if you visit the url bar/baz the function page_example_baz will be called. This is incorrect. If you specify the % the argument will be required for the function to be called.

Could you give a correct

chris_car - Tue, 2009-10-27 20:28

Could you give a correct example? I cannot make this example work either.

Thanks!

Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.