What is the path of my module in Drupal 7?

2019-09-10 16:58发布

This is my first time with Drupal 7, i wrote a test module that echo "test" , and in the menu the path is "test".

When i'm trying to access localhost/drupal/test OR localhost/drupal/admin/test it can't find it, and i see 404 or admin page.

What is the problem here?


this is the code, and its doesnt work

<?php
/* $Id$ */

/**
* @file
* Very simple DRUPAL module
*/

/**
* Implementation of hook_help().
*/
function hello_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...';
  }
}

/**
* Implementation of hook_menu().
*/
function hello_world_menu($may_cache) {
  $items = array();

  if ($may_cache) {
  }
  else {
    $items[] = array(
      'path' => 'hello', // drupal path example.com/?q=hello
      'title' => 'Hello world page...', // page title
      'callback' => 'hello_world_page', // callback function name
      'access' => TRUE, // every user can look at generated page
      'type' => MENU_CALLBACK // define type of menu item as callback
    );
  }

  return $items;
}


/**
* Function which generate page (this generate any content - you need only your own code...)
*/
function hello_world_page() {
  return '<p>Hello world!</p>';
}
?>

标签: drupal
2条回答
时光不老,我们不散
2楼-- · 2019-09-10 17:25

Use drupal_get_path() to get the path of your module or theme. If the name of your module is 'mymodule', then you'd simply invoke the below piece of code to get the path.

drupal_get_path('module', 'mymodule');

EDIT

Reading your question again makes me realise that your'e asking for the URL to your module. Could you please post the code you've got in your module's hook_menu()?

查看更多
可以哭但决不认输i
3楼-- · 2019-09-10 17:39

If I am getting you correctly,

  1. you wrote a hook_menu() where you created a menu path test(say $item['test']) and wrote a page callback function.

  2. In the page callback function, you say echo test.

If this is the scenario, you just need to clear your cache. Drupal needs to register the menu item. After writing any menu hook implementation, you should clear your cached data and then try again.

查看更多
登录 后发表回答