与词的Drupal url通配符

时间:2012-08-21 12:07:20

标签: url drupal wildcard

可以在菜单中使用这样的通配符吗?

$items['foo/bar-%xxx']
...
'page arguments' => array(1),

所以我可以讨论一下吧?

2 个答案:

答案 0 :(得分:2)

不要让这个令人反感的回复,但不要做danielson317建议的那样做。 原因是,通过这种方式,您将有几个不必要的菜单路由器项目,将破坏您的网站性能。其次,我们通常不会这样做。

您可以通过将主回调注册到您的函数来执行此操作。

$items['foo/%']
...
'page arguments' => array(1),

在回调函数中,您可以检查给定的参数是否有效。

function MYMODULE_foo_bar($value){
if (substr($value, 0, 4) != 'bar-'){
drupal_not_found();
return; // not necessary though.
}
$value = substr($value, 5);
// $value is now the the desired value.
//do what you want and return the output.
}

答案 1 :(得分:0)

根据hook_menu http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_menu/6

上的文档

%必须是条目的第一个字符。它可以是单独的,也可以是一个预先加载运算符的字符串。

所以不,你不能以这种方式提出论据。

但是,您可以使用循环在hook_menu中创建多个条目。

$results = some_query_function();
foreach ($results as $result) {
  $items['foo/bor-' . $result] = array();
}

你可以混合两者:

$results = some_query_function();
foreach ($results as $result) {
  $items['foo/' . $result] = array();
  $itesm['foo/bar-' . result . '/%/edit'] = array(
  ...
  'page arguments' => array(2),
  );
}