Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6355229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:43:25+00:00 2026-05-24T22:43:25+00:00

In drupal I want to define a URL without hook_menu() , so that I

  • 0

In drupal I want to define a URL without hook_menu(), so that I can link the URL to a function that will be return a JSON (I want to call it from JavaScript code).

function(){
  $arr = array('a'=>'one', ..........);

  return json_encode($arr);
}

Let me know how I can define a URL without hook_menu(), and link it to a function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:43:26+00:00Added an answer on May 24, 2026 at 10:43 pm

    Yes, but No

    If you absolutely must hack your way around something to call a specific php file. Like if you actually have a file at /somepath/yourscript.php this could be callable from the general web. From there, you’d have to bootstrap Drupal yourself if you wanted anything in the Drupal namespace, or you’d have to use require or include for each file where you wanted to call something… but it’s a bad idea.

    To bootstrap Drupal you’d need something like this:

    include_once 'path.to.../includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    

    At which point you are welcome to blow your own face off with whatever monstrosity you create. Hopefully, you’ve noticed that by bootstrapping Drupal you probably don’t save any time doing what you wanted in the first place by putting some rogue script in your file system.

    The Drupal Way

    Instead, use what Drupal gives you… A menu hook implemented as a MENU_CALLBACK. This will take a request right off the wire and send it to your function, as you desire. In your function, nothing stops you from looking at $_POST and $_GET variables, but you do get some Drupal sugar, in that your MENU_CALLBACK can accept URL parameters, check their validity, and pass them into your function so you have some assurances that you are working with valid data.

    You need only define your menu hook and your callback function. To use validation, you can use a named argument, like %arg instead of the % in the example below. If you use the named parameters, you then would create a function called arg_load($arg) which would make sure you have a valid parameter, then return a value to be passed into your callback_function. Here’s what your Drupal code might look like:

    function your_module_menu(){
        $items['js/calls/this/path/%'] = array(
          'title' => 'Some callback',
          'description' => 'Will call callback_function',
          'page callback' => 'callback_function',
          'page arguments' => array(4),
          'access arguments' => array('access content'),
          'type' => MENU_CALLBACK
       );
    
    // moar code here!
    
    return $items;
    }
    
    function callback_function($url_var){
      // drupal_json_output manages your content-type header
      // and the json_encode for you.
      drupal_json_output($url_var);
    }
    

    I think the Drupal way is much better than the hacky one off file way for a few reasons. One, developers will understand what you are doing if you go the Drupal way. Two, you can change the URL as needed.

    The only reason I’d go for the standalone file is if, the file has nothing to do with anything you need out of Drupal AND that action had to be damn fast. Like so fast the difference in time between bootstrapping Drupal and not would take too damn long. In that case I’d probably use apache or nginx to forward a URL path to the file somewhere else in the filesystem. And if this is the case, I’ll gladly eat my humble pie.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display the query that is executed in the drupal view. Currently
I'm new at Drupal. my url is like www.blabla.com/drupal/blog/ I have some blocks that
I'm writing a Drupal module that deals with creating new nodes from CSV files.
I want to find a drupal theme that has a bar like Twitter's top
I want to add cron jobs to my Drupal 7 system, but I can't
In Drupal, if you want to insert something into the node table, 'nid' can
I'm executing a query which I want to randomize query results from a drupal
I want to create a drupal website which will be in English but with
I want to write Drupal Form value made from API Form, to my database
I want to install Drupal in Ubuntu. How can I do it?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.