Get program name in drupal
I would like to get the name of a program in Drupal (actually the name in the url that calls the program or function).
Example: http: // localhost / drupal6 / my_widget
It works:
$myURL = (basename($_SERVER['REQUEST_URI'])); // $myURL=my_widget
but not when I have additional parameters:
http://localhost/drupal6/my_widget/parm1/parm2
a source to share
You can use parse_url to extract part of the url path, and then you can select an item from the exploded parts. For instance:
$x=parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$y=explode('/',$x);
$z=$y[2];
More information can be found here: http://www.php.net/manual/en/function.parse-url.php
a source to share
In most cases, you can and should use hook_menu to handle the URL and the parameters in it. Take a look at the following code example 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. This example registers two paths: "example / foo" and "example / baz /% /%". The first path is a simple path with no parameters; on request, 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 "correct way" to have a URL with parameters.
a source to share