我编写了一个简单的测试模块示例,2个文件,test.module,test.info,并在drupal 7模块中启用了它们。
我清除了所有缓存,但当我尝试访问 localhost/drupal/hello 时,我得到 drupal 404 页面未找到,这是为什么?
这是我的代码:
<?php
function test_world_help($section) {
switch ($section) {
case 'admin/help#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>';
}
你已经发布了几乎相同的问题once https://stackoverflow.com/questions/7489612/drupal-7-how-i-can-acces-my-module-and-view-admin-page and twice https://stackoverflow.com/questions/7510465/what-is-the-path-of-my-module-in-drupal-7前。为什么不更新原来的内容而是发布新的内容?
在 Drupal 7 中,hook_menu() 没有 $may_cache 参数。您应该将其删除。但是,它不应该解决您的问题,因为它未设置且错误。因此,$items 仍应被填充。
正如 jprofitt 所说,您应该将“回调”更改为“页面回调”,这是正确的。
没有“访问”这样的东西,但有“访问回调”和“访问参数”。您很可能正在寻找“访问回调”。但是,您不能将其设置为“true”。它需要一个返回“true”或“false”的函数名称。它默认为“user_access”,因此您应该保持这种状态。但是,您可能希望将“访问参数”设置为“访问内容”之类的内容。
下面这段代码是不是效果更好?
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;
}
看来你还没有真正看过文档 http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_menu。我可能错了。然而,当您想了解某些东西如何工作的基础知识时,api.drupal.org 上的文档始终是一个很好的开始。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)