Drupal 7 自定义模块给出 403


Drupal 7 custom module gives 403

我的自定义Drupal 7模块遇到了一些问题。请注意,这不是我的第一个模块。这是我hook_menu;

function blog_contact_menu(){
    $items = array();
    $items["blog_contact/send_to_one"] = array(
    "page_callback"=>"single_blogger_contact",
    "access_arguments"=>array("access blog_contact content"),
    "type"=>MENU_CALLBACK
    );
    return $items;
}

这是我的烫发函数;

function blog_contact_perm() {
    return array("access blog_contact content");
}

这应该有效,但是当我进行ajax调用时,它会禁止403。您无权查看bla bla。我的 ajax 调用正确且简单,url 正确,类型为 post。我没有直接看到原因。

菜单路由器项中的属性中包含空格而不是下划线。 access_arguments实际上应该是access arguments的,page_arguments应该是page arguments的,等等:

function blog_contact_menu(){
  $items = array();
  $items["blog_contact/send_to_one"] = array(
    "title" => "Title",
    "page callback"=>"single_blogger_contact",
    "access arguments"=>array("access blog_contact content"),
    "type"=>MENU_CALLBACK
  );
  return $items;
}

另请注意,title是必需属性。

除此之外,已经提到的hook_permission()问题,您的代码是正确的。

由于您没有在hook_menu实现中指定access_callback,因此默认情况下它使用 user_access 函数并检查您是否授予了access blog_contact content权限。

function blog_contact_menu(){
    $items = array();
    $items["blog_contact/send_to_one"] = array(
    // As mentioned in Clive's answer, you should provide a title 
    "title" => "Your Title goes here",
    "page callback"=>"single_blogger_contact",
    // No "access callback" so uses user_access function by default
    "access arguments"=>array("access blog_contact content"),
    "type"=>MENU_CALLBACK
    );

access blog_contact content不是Drupal知道的权限,因此user_access函数返回false,这就是您被拒绝403访问的原因。

如果你想告诉Drupal关于access blog_contact content权限,那么钩子是hook_permission,而不是hook_perm

你的代码应该更像:

function blog_contact_permission() {
  return array(
    'access blog_contact content' => array(
      'title' => t('Access blog_contact content'), 
      'description' => t('Enter your description here.'),
    ),
  );
}