【问题标题】:Drupal 6 absolute wildcards in _menu(), is it possible?_menu()中的Drupal 6绝对通配符,可能吗?
【发布时间】:2009-10-02 10:29:04
【问题描述】:

是否可以按模块处理 _menu() 中的所有通配符。

我知道特定的通配符,例如

display/page/% 但这不适用于路径display/page/3/andOrderBy/Name

如果我想处理不可预测的参数数量,比如

display/page/3/12/45_2/candy/yellow/bmw/turbo

我想要一个display/* _menu() 路径来处理所有参数。

我该怎么做?

【问题讨论】:

    标签: drupal drupal-routes


    【解决方案1】:

    Drupal 会将任何附加的 URL 元素作为附加参数传递给您的 hook_menu 回调函数 - 在您的回调中使用 func_get_args() 来获取它们。

    所以如果你只注册了一个通配符display/page/%,但实际的请求有两个额外的元素display/page/3/andOrderBy/Name,你的回调将作为显式参数传递'3',但也将'andOrderBy'和'Name'作为隐式传递其他的。

    回调示例:

    function yourModuleName_display_callback($page_number) {
      // Grab additional arguments
      $additional_args = func_get_args();
      // Remove first one, as we already got it explicitely as $page_number
      array_shift($additional_args);
      // Check for additional args
      if (!empty($additional_args)) {
        // Do something with the other arguments ...
      }
      // other stuff ...
    }
    

    【讨论】:

      【解决方案2】:

      啊 ;) 你是对的

      这就是我解决它的方法。

      function mysearch_menu() {
      $items['mysearch/%'] = array(
      'page callback' => 'FN_search',
      'access callback' => TRUE,
      );
      return $items;
      }
      
      
      function FN_search()
      {
          return print_r(func_get_args(),true);
      };
      

      【讨论】:

      • 顺便说一句,Drupal 中的标准做法是在所有函数前面加上模块名称以防止命名冲突,因此如果您的模块称为“mysearch”,则您的回调应命名为 mysearch_search 或 @ 987654323@。否则向现有实例添加新模块可能会破坏站点。
      猜你喜欢
      • 2014-10-30
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      相关资源
      最近更新 更多