1

In our project, we have this module meant strictly for our custom API endpoints for our mobile applications to use. Currently, this is handled in a single .module file like so:

my_project_mobile_api.module

<?php

function my_project_mobile_api_menu() {
  return array(
    'api/v1/user/register' => array(
      'page callback' => 'account_register',
      'access callback' => 'user_is_logged_in'
      'type' => MENU_CALLBACK
    ),
    ...
  );
}

function account_register() {
  ... logic to create an account
}

Up until now, this has worked great for us, but now we need to add a V2 of the API. My idea was to extract all of the page callback functions (like account_register) into their own .inc file. This would look like this:

my_project_mobile_api.module

function my_project_mobile_api_menu() {
  return array(
    'api/v1/user/register' => array(
      'page callback' => 'account_register',
      'access callback' => 'user_is_logged_in'
      'type' => MENU_CALLBACK
    ),
    'api/v2/user/register' => array(
      'page callback' => 'account_register',
      'access callback' => 'user_is_logged_in'
      'type' => MENU_CALLBACK
    ),
    ...
  );
}

my_project_mobile_api.v1.inc

function account_register() {
  ... logic to create an account
}

my_project_mobile_api.v2.inc

function account_register() {
  ... logic to create an account (this logic differs from that of V1)
}

How can I make sure that my .module uses the right account_register function from the correct .inc file? Is there a way to specify the file in the api/v1/user/register array?

3

You can use file key in your menu like:

I assume you have a includes directory contain both files my_project_mobile_api.v1.inc and my_project_mobile_api.v2.inc and both files contain account_register function.

Your hook_menu should be like the following.

function my_project_mobile_api_menu() {
  return [
    'api/v1/user/register' => [
      'page callback'   => 'account_register',
      'access callback' => 'user_is_logged_in',
      'type'            => MENU_CALLBACK,
      'file'            => 'includes/my_project_mobile_api.v1.inc',
    ],
    'api/v2/user/register' => [
      'page callback'   => 'account_register',
      'access callback' => 'user_is_logged_in',
      'type'            => MENU_CALLBACK,
      'file'            => 'includes/my_project_mobile_api.v2.inc',
    ],
  ];
}

You can also use 'file path' if you files not in the same module take a look at this example.

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.