I asked this question in the general area and didn't get a response then realized it was probably because I asked in the wrong place. Sorry in advance for the duplicate post.

I'm writing a menu hook and I want to get the current path so I can create a dynamic menu from it. I've found two ways of getting it thus far:

$local_path = str_replace(base_path(),"",request_uri());
$local_path = substr(request_uri(),strlen(base_path())) ;

For request_uri is "/drupal/mypath/mysubpath" $local_path will be "mypath/mysubpath."
For request_uri is "/mypath/mysubpath" $local_path will also be "mypath/mysubpath."

But it seems like Drupal would have that information available more directly. Is there a global variable or a function I can call to get this? Thanks in advance.

-Mike

Comments

caligari’s picture

Try this (not tested):

<?php

function drupal_current_path () {
  $path = array();
  $a = 1;
  while ($argument = arg($a++)) $path[] = $argument;
  return (count($path) ? implode('/', $path) : variable_get('site_frontpage', 'node'));
}

?>
mikeschinkel’s picture

Thanks for the follow up, but wouldn't that be a lot more compute intensive than even the version I suggested and was trying to streamline?

peterpoe’s picture

Try using the function arg.