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 kn开发者_Python百科ow how I can define a URL without hook_menu()
, and link it to a function.
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.
You cannot. Every URL exposed by a module is exposed through hook_menu()
. That is the only way to let Drupal know which URLs are handled by a module.
There are many Drupal functions that return a JSON object and that are associated to URLs defined in hook_menu()
; for example, poll_choice_js() is the page callback for poll/js. The fact the URL is defined through hook_menu()
doesn't make any difference for the JavaScript code that uses the URL to get the JSON object it needs.
Agreed. You can not. In drupal there are a few ways that a url is exposed.
- A physical file exists, and therefore you can access it directly (tho this would be outside of drupal most likely)
- Via hook_menu
- Via views page callbacks
- Via url alias
- ... and a couple other modules which handle urls...
The reason this is so is because the urls that you are likely referring to don't really exist. The real path of path/json is actually index.php?q=path/json to the apache server
If you want to have a url just output JSON code.. finish the page with a call to: drupal_json(..) [http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_json/6]
精彩评论