views:

11

answers:

3

I would like to get program name in Drupal (actually name in URL that calls program or function).

example: http://localhost/drupal6/my_widget

This works:

$myURL = (basename($_SERVER['REQUEST_URI']));  //  $myURL=my_widget

but not when I have additional parameters:

http://localhost/drupal6/my_widget/parm1/parm2
A: 

You probably want to use arg()

Justintime
The Drupal API documentation says about the arg() function: "Avoid use of this function where possible, as resulting code is hard to read. Instead, attempt to use named arguments in menu callback functions." See http://api.drupal.org/api/function/arg/6.
marcvangend
+1  A: 

You can use parse_url to extract the path part of the url and then you can pick the item from the exploded parts. For example:

$x=parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$y=explode('/',$x);
$z=$y[2];

More info can be found here: http://www.php.net/manual/en/function.parse-url.php

zaf
A: 

In most cases, you can and should use hook_menu to handle the url and the parameters in it. Have a look at the following sample code, taken from 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;
}

This function is an implementation of hook_menu. In this example, two paths are registered: 'example/foo' and 'example/baz/%/%'. The first path is a simple path without parameters; when requested, the function page_example_foo() is called without parameters. The second path is a path with two parameters ('arguments'). When this path is requested, Drupal will run the function page_example_baz($argument1, $argument2). This is considered 'the right way' to have url's with parameters.

marcvangend