开发者

Drupal 7 example module, page not found, why?

开发者 https://www.devze.com 2023-04-07 04:37 出处:网络
I wrote a simple test module example, 2 files, test.module, test.info, and enabled them in drupal 7 modules.

I wrote a simple test module example, 2 files, test.module, test.info, and enabled them in drupal 7 modules.

I cleared all the cache, and still when i'm trying to go to localhost/drupal/hello , i get drupal 404 page not found, why is that?

Here is my code:

<?php

function test_world_help($section) {
  switch ($section) {
    case 'admin/he开发者_如何学Golp#hello_world':
      $output = '<p>Hello world help...</p>';
      return $output;
    case 'admin/modules#description':
      return 'Hello world module description...';
  }
}

function test_world_menu($may_cache) {
  $items = array();

  if ($may_cache) {
  }
  else {
    $items['hello'] = array(
      'title' => 'Hello world page...', 
      'callback' => 'test_world_page', 
      'access' => TRUE, 
      'type' => MENU_CALLBACK 
    );
  }

  return $items;
}

function test_world_page() {
  return '<p>Hello world!</p>';
}


You have posted almost the same question once and twice before. Why don't you update the original one instead of posting new ones?

  • The hook_menu() does not have the $may_cache argument in Drupal 7. You should remove it. However, it should not solve your problem as it is unset and false. Thus, the $items should still be populated.

  • It is correct, as jprofitt says, that you should change 'callback' to 'page callback'.

  • There is no such thing as 'access', but there is 'access callback' and 'access arguments'. You are most likely looking for 'access callback'. However, you can't just set it to 'true'. It expects a function name which returns either 'true' or 'false'. It defaults to 'user_access', so you should just leave it that way. However, you might want to set 'access arguments' to something like 'access content'.

Does the following piece of code work better?

function test_world_menu() {

  $items = array();

  $items['hello'] = array(
    'title' => 'Hello World!', 
    'page callback' => 'test_world_page', 
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK 
    );

  return $items;
}

It seems that you haven't really had a look at the documentation. I might be wrong. However, the documentation at api.drupal.org is always a good start to look when you want to learn the basics of how something work.


You should probably change 'callback' to 'page callback', as I don't believe hook_menu() has just a plain "callback" option. And since you're working with Drupal 7, its hook_menu() actually doesn't have parameters.


Uninstall and reinstall your custom module. I hope this will help you. Because it's necessary for drupal core to know the newly created path created using hook_menu.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号