访问参数和访问回调之间有什么区别?


21

访问参数和访问回调之间有什么区别?

假设我将访问回调设置为'user_access',并且array('custom permission')是访问参数的值。

这意味着什么?它们如何相互影响?

Answers:


27

“访问回调”是用来验证用户是否有权访问页面的函数。作为特殊情况,它可以是value TRUE,在这种情况下,所有用户都可以访问它;换句话说,访问许可将被绕过。

如果您使用函数名称作为“访问回调”值(默认情况下为“ user_access”),则还可以使用“访问参数”,它是一个包含传递给访问回调函数的参数的数组。
与其他菜单回调一样,参数必须是字符串或数字。如果是数字,则该值将替换为从菜单路径中获取的值。如果要避免这种替换,则需要使用字符串代替数字。例如,将"1"一个用作传递给访问回调的参数将避免自动替换。

这些是Drupal核心模块中使用的菜单回调声明的一些示例。(这些示例来自Drupal 7代码,但是对于我想指出的一点,这没有任何区别。)

这是一个例子,其中接入回调user_access() 。

  $items['file/progress'] = array(
    'page callback' => 'file_ajax_progress', 
    'delivery callback' => 'ajax_deliver', 
    'access arguments' => array('access content'), 
    'theme callback' => 'ajax_base_page_theme', 
    'type' => MENU_CALLBACK,
  );

这是访问回调不是函数名的示例。

  $items['user'] = array(
    'title' => 'User account', 
    'title callback' => 'user_menu_title', 
    'page callback' => 'user_page', 
    'access callback' => TRUE, 
    'file' => 'user.pages.inc', 
    'weight' => -10, 
    'menu_name' => 'user-menu',
  );

在这种情况下,访问回调是user_view_access(),而不是数字1,而是从菜单路径中获取的值(在本例中为“ user /%user”);这是一种特殊情况,因为该函数将获取所返回的值user_load()。

  $items['user/%user'] = array(
    'title' => 'My account', 
    'title callback' => 'user_page_title', 
    'title arguments' => array(1), 
    'page callback' => 'user_view_page', 
    'page arguments' => array(1), 
    'access callback' => 'user_view_access', 
    'access arguments' => array(1),
    // By assigning a different menu name, this item (and all registered child
    // paths) are no longer considered as children of 'user'. When accessing the
    // user account pages, the preferred menu link that is used to build the
    // active trail (breadcrumb) will be found in this menu (unless there is
    // more specific link), so the link to 'user' will not be in the breadcrumb. 
    'menu_name' => 'navigation',
  );

假设先前的菜单定义如下,并使用“ user / hello”之类的路径进行调用。

  $items['user/%'] = array(
    'title' => 'My account', 
    'title callback' => 'user_page_title', 
    'title arguments' => array(1), 
    'page callback' => 'user_view_page', 
    'page arguments' => array(1), 
    'access callback' => 'user_view_access', 
    'access arguments' => array(1),
    // By assigning a different menu name, this item (and all registered child
    // paths) are no longer considered as children of 'user'. When accessing the
    // user account pages, the preferred menu link that is used to build the
    // active trail (breadcrumb) will be found in this menu (unless there is
    // more specific link), so the link to 'user' will not be in the breadcrumb. 
    'menu_name' => 'navigation',
  );

在这种情况下,访问回调将接收从路径中获取的值作为参数(0表示“用户”,而1表示“用户”之后的部分和斜杠);在这种情况下,该值为“ hello”。

为了更好地理解这些通配符参数,请参见通配符加载程序参数。该文档页面被标记为Drupal 6,但所报告的内容仍适用于Drupal 7。


很棒的解释。在我的要求中,某些自定义按钮“立即投票”应仅由经过身份验证的用户处理,但按钮也应显示为匿名用户,当匿名用户单击该按钮时,他应重定向到登录页面,然后继续进行。对于此特定方案,是否有特殊的访问条件才能显示匿名按钮?在这里发表评论,因为它会使这篇文章更加完整。已收藏书签.. :)
— 2015年

4

访问回调是一种检查某些用户是否具有某些权限的功能。默认的访问回调是user_access()

访问参数列出了由访问回调检查的权限。例如“访问内容”


还有什么价值access callback呢?如果不是user_access(),那是否会消除对它的需要access arguments?
— user1359 2011年

2
无论你想要什么。任何返回TRUE或FALSE的函数。
— Berdir 2011年

因此,您不需要access arguments吗?
— user1359 2011年

是的,那么您可以跳过它
— Shoaib Nawaz
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.