drupal--hook_menu

痞子三分冷 提交于 2019-12-08 14:26:19

问题


the info file is right,the following is my module file code. when i access the http://localhost/drupal/mymenu why it can't work.

  <?php
 function mymenu(){
    $item = array();
  $item['mymenu'] = array(
       'description'=>'test1',
      'page callback'=>'mymenu_test',
      'access arguments' => array('access mymenu'),
     'type'=>MENU_CALLBACK,
 );
 return $item;
 }

  function mymenu_perm(){
   return array('access mymenu');
  }

 function mymenu_test() {
 $output = 'hello world';
 return $output;
}

i have gave the 'access mymenu' permission to the anonymous.


回答1:


It should be

function mymenu_menu() { ... }

You don't need the $item = array(); there also.




回答2:


whenever you see a api function with hook_something, you have to replace the 'hook' part with the name of your module

in this case it's indeed mymenu_menu




回答3:


Try the following: use function name as modulename_menu and use 'access arguments' => array('access content').

<?php
  function test_menu(){
  $item = array();
  $item['mymenu'] = array(
  'description'=>'test1',
  'page callback'=>'mymenu_test',
  'access arguments' => array('access content'),
  'type'=>MENU_CALLBACK,
  );
  return $item;
  }

  function mymenu_test() {
  $output = 'hello world';
  return $output;
  }



回答4:


you need to flush menu cache(at least two times in drupal 7) after adding menu item with hook_menu.



来源:https://stackoverflow.com/questions/4192717/drupal-hook-menu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!