我想在Drupal中獲得程序名稱(實際上是調用程序或函數的URL中的名稱)。在drupal中獲取程序名稱
例如: http://localhost/drupal6/my_widget
這工作:
$myURL = (basename($_SERVER['REQUEST_URI'])); // $myURL=my_widget
而不是當我有額外的參數:
http://localhost/drupal6/my_widget/parm1/parm2
我想在Drupal中獲得程序名稱(實際上是調用程序或函數的URL中的名稱)。在drupal中獲取程序名稱
例如: http://localhost/drupal6/my_widget
這工作:
$myURL = (basename($_SERVER['REQUEST_URI'])); // $myURL=my_widget
而不是當我有額外的參數:
http://localhost/drupal6/my_widget/parm1/parm2
你可能想使用arg()
您可以使用PA rse_url提取URL的路徑部分,然後您可以從爆炸部件中選取項目。例如:
$x=parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$y=explode('/',$x);
$z=$y[2];
更多信息可以在這裏找到:http://www.php.net/manual/en/function.parse-url.php
在大多數情況下,你可以和應該使用hook_menu處理URL和它的參數。看一看下面的示例代碼,從http://api.drupal.org/api/function/page_example_menu/6採取:
function page_example_menu() {
// This is the minimum information you can provide for a menu item. This menu
// item will be created in the default menu: Navigation.
$items['example/foo'] = array(
'title' => 'Page example: (foo)',
'page callback' => 'page_example_foo',
'access arguments' => array('access foo content'),
);
// By using the MENU_CALLBACK type, we can register the callback for this
// path but do not have the item show up in the menu; the admin is not allowed
// to enable the item in the menu, either.
//
// Notice that the 'page arguments' is an array of numbers. These will be
// replaced with the corresponding parts of the menu path. In this case a 0
// would be replaced by 'bar', a 1 by 'baz', and like wise 2 and 3 will be
// replaced by what ever the user provides. These will be passed as arguments
// to the page_example_baz() function.
$items['example/baz/%/%'] = array(
'title' => 'Baz',
'page callback' => 'page_example_baz',
'page arguments' => array(2, 3),
'access arguments' => array('access baz content'),
'type' => MENU_CALLBACK,
);
return $items;
}
此功能hook_menu的實現。在這個例子中,註冊了兩個路徑:'example/foo'和'example/baz /%/%'。第一個路徑是一個沒有參數的簡單路徑;當請求時,函數page_example_foo()
被調用,不帶參數。第二個路徑是具有兩個參數('參數')的路徑。當請求這條路徑時,Drupal將運行函數page_example_baz($argument1, $argument2)
。這被認爲是「正確的方式」,使網址的參數。
Drupal API文檔中提到了arg()函數:「儘可能避免使用此函數,因爲結果代碼很難讀取,而是嘗試在菜單回調函數中使用命名參數。請參閱http://api.drupal.org/api/function/arg/6。 – marcvangend 2010-05-03 19:36:40