7

I implemented hook_menu() to create a new path called 'user-login'. The page callback function receives url parameters like $_GET['parameter']; however, when I go to http://site.com/user-login?parameter=test, I get a 404 error. How should I implement hook_menu() to get this working?

This is the code I'm using

/**
 * Implementation of hook_menu()
 */
function login_menu() {
  //$items = array();

  $items['user-login'] = array(
    'title' => 'External login',
    'page callback' => 'user_login_callback',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function user_login_callback() {

  // Get the URL params
  $uid = urldecode($_GET['uid']);
  $expiry = urldecode($_GET['expiry']);
  $provided_hash = urldecode($_GET['hash']);  

}

Page would be called like http://site.com/user-login?uid=2?expiry=3?hash=sd7wdah

  • May you provide the code used in the implementation of hook_menu()? – kiamlaluno Jun 3 '11 at 3:43
  • I added the code – Matthew Hui Jun 3 '11 at 3:52
4

The drupal menu system does not care about $_GET arguments, you can do whatever you want with them, it doesn't affect the hook_menu() implementation **.

So, the error lies somewhere else. The result of hook_menu() is stored, it is not called on every page. If you change/add anything in that function, you need to rebuild the menus. The fastest way to do that is using the drush command "drush cc all" or "rebuild menus" in devel.module's Development links block. You can also submit the modules page, though.

** That said, it is much more common to pass arguments through the menu system as additional path arguments. Anything passed after the path will be passed as arguments to your page callback automatically. So calling user-login/2/3/sd7wdah will pass these three arguments to your function as user_login_callback(2, 3, 'sd7wdah'). You can also make these arguments required by specifying the path like this in hook_menu(): user-login/%/%/% and defining the path arguments like this: 'path arguments' => array(1, 2, 3),. This will make sure that your function can only be called with these three arguments and that they aren't optional.

  • I implemented it exactly as you said but got warning: Missing argument 1 for user_login_callback() Missing argument 2 for user_login_callback() Missing argument 3 for user_login_callback() – Matthew Hui Jun 3 '11 at 8:07
  • So instead of user-login/%/%/% I kept it as 'user-login' and added 'path arguments' => array(1, 2, 3), and it worked. Thanks – Matthew Hui Jun 3 '11 at 8:18
1

Just for clarity,

http://site.com/user-login?uid=2?expiry=3?hash=sd7wdah

will not work, it should be:

http://site.com/user-login?uid=2&expiry=3&hash=sd7wdah

Note how there's only one "?" and each additional parameter is separated with "&"

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.