2

I'm working on my first module. I'm setting up hook_menu() to register several URLs. Some of the parameters may be raw strings with slashes in them. For example, "Round 1 / Heat 1" as one parameter. Now, I know that I can make my javascript encode the slashes when constructing the URL, but it makes me wonder if I'm Doing It Wrong™.

It feel natural to me to be able to use GET params, but I don't see any way to access those in menu callbacks.

Am I doing It wrong with these URL-based parameters?
Is it possible to just POST these parameters, and access them in menu callbacks?

4

There's one additional trick additionally to the options that you already listed. If you look at search_menu(), you can see that it uses a named placeholder called %menu_tail and this: 'load arguments' => array('%map', '%index'),.

This does exactly what the name suggests. It calls menu_tail_load() (and passed it's own index/position to it as a load argument), which doesn't actually load anything but takes all additional path elements including it's own position, and returns them as a single argument.

Obviously, this only works for the last argument. If you have multiple arguments like this, use GET/POST variables or escape them.

  • I ended up just writing a reusable javascript function to escape all of my url parts. – Kenny Wyland Aug 9 '12 at 23:40
1

You could access the parameters in hook_menu(). Try the following code.

function menufun_menu() {
  $items['menufun/%/bar/baz'] = array(
    'title' => 'Hi',
    'page callback' => 'menufun_hello',
    'page arguments' => array(1), // The matched wildcard.
    'access callback' => TRUE,
  );

  return $items;
}

/*
 * Page callback.
 */
function menufun_hello($a = NULL) {
  return t('Hello. $a is @a', array('@a' => $a));
}

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.