簡體   English   中英

Drupal 7示例模塊,找不到頁面,為什么?

[英]Drupal 7 example module, page not found, why?

我編寫了一個簡單的測試模塊示例,包含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>';
}

您之前曾經 兩次張貼幾乎相同的問題。 為什么不更新原始的而不發布新的呢?

  • hook_menu()在Drupal 7中沒有$ 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;
}

看來您還沒有真正閱讀過該文檔 我可能錯了。 但是,當您想了解某項工作原理的基礎時,api.drupal.org上的文檔始終是一個不錯的開始。

您可能應該將'callback'更改為'page callback' ,因為我不相信hook_menu()只有一個簡單的“ callback”選項。 而且由於您正在使用Drupal 7,因此它的hook_menu()實際上沒有參數。

卸載並重新安裝自定義模塊。 我希望這能幫到您。 因為drupal核心必須知道使用hook_menu創建的新創建的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM