I'm writing a custom drupal module and have just starter out working with Drupal.

I need to expose a custom URL from my module so that some JavaScript files can request this URL for XML data. How can I do this? I'm not referring to the hook menu. Thanks

  • 1
    hook_menu is the Drupal way to define URL. You can try services Module also. – niksmac Nov 1 '12 at 7:21

You need hook_menu in your .module file.

This hook enables modules to register paths in order to define how URL requests are handled. Paths may be registered for URL handling only ...


function MY_MODULE_menu() {
  $items['path/callback/%/%'] = array(
    'page callback' => 'my_callback_func',
    'page arguments' => array(2, 3),

  return $items;

function my_callback_func($arg2, $arg3) {
  //your process here
  $data = array($arg2, $arg3);
  return drupal_json_output($data);

in js call this url with 2 arguments: path/callback/VAR_1/VAR_2 - VAR_1 is second argument, VAR_2 is third as defined in "'page arguments' => array(2, 3)" (path is zero argument, callback is first argument)

The $data may be returned in what you want format. In this example this is JSON.

More for hook_menu - http://api.drupalize.me/api/drupal/function/hook_menu/7

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.