Drupal 7 开发内部培训资料, 分头诗人

hook_menu中的access callback和access arguments不是很清楚

赞成!
0
否决!
<?php
/**
 * Implementation of hook_menu().
 */

function menufun_permission() {
  return array(
   'receive greeting' => array(
      'title' => t('Receive a greeting'),
      'description' => t('Allow users receive a greeting message'),
    ),
  );
}

function menufun_menu() {
  $items['menufun'] = array(
    'title' => 'Menu Fun',
    'page callback'   => 'menufun_greeting',
    'file'  => 'menufun_greeting.inc',
    'access callback' => 'user_access',
    'access arguments' => array('receive greeting'),
    'type'   => MENU_NORMAL_ITEM,
    'weight' => 99
);

 

一个未登录的用户执行http://example.com/?q=menufun会报Access denied,

那什么时候才会显示"Allow users receive a greeting message"?

2 个回答

赞成!
0
否决!

这是Pro Drupal7里面的例子 

看不懂menufun_permission()是想干什么

原话

In our examples so far, we’ve simply set the access callback key of the menu item to TRUE, meaning that
anyone can access our menu. Usually, menu access is controlled by defining permissions inside the
module using hook_permission() and testing those permissions using a function. The name of the
function to use is defined in the access callback key of the menu item and is typically user_access. Let’s
define a permission called receive greeting; if a user does not have a role that has been granted this permission, the user will receive an “Access denied” message if he or she tries to go to http://example.com/?q=menufun.

赞成!
0
否决!

access callback 后面的参数是一个函数名,如你上面的例子,对应的参数是user_access,那么就必定有 function  user_access(),

access arguments 是函数的参数,一个参数就只有一个,如这里只有一个参数,所以函数名应该是有个参数的,如function  user_access($something),

access callback 对应的函数,返回TRUE or FALSE,TRUE是有权限,FALSE就是没权限,就这么简单。