I'm trying to find a way to pass URL arguments to a node where I generate the content using custom php code.

Lets say the node is at node/15, currently aliased to foobar/. I want the ability to call foobar/1, foobar/2, etc, and have that value after the slash passed on to my php code. If possible, I'd also like the ability to call the very same php code at just foobar/ without passing an argument.

What's the best way to get this done?

Comments

cog.rusty’s picture

If you were not looking for aliased paths you could use arg(0), arg(1), arg(2) etc

Here, I think you can use drupal_get_path_alias($_GET['q']) and check the parts of the returned aliased path in your code.

Nosln’s picture

How would I be able to configure the argument-containing urls to direct to the original page? Currently, if I go to foobar/, the page loads properly, but if I go to foobar/1, I get "Page not found". Is there any way to wildcard path aliases so that foobar/* always sends the request to foobar/ while also passing the arguments found in *?

cog.rusty’s picture

Sorry, I misunderstood the question.

In Drupal 5, foobar/1, foobar/2 etc don't go to foobar, where you have your code. In 4.7 that was happening, but it was a bug.

Also, you can't re-alias them all to go to that same page. You could try to use apache rewrites in .htaccess, but then you may lose the /1 or /2 parameters after the rewrite.

Perhaps you can find a way to pass /1 or /2 not as a path, but
- as an internal page anchor foobar#chapter2 (as in <a href="#chapter2">)
- or as a query string (like the ?q= which Drupal uses, but a different one, not affected by clean urls)

I don't know much about these methods, but you could search how existing modules do this kind of things.

Nosln’s picture

I ended up solving the problem by adding the following to the beginning of my .htaccess:

RewriteRule ^foobar/([0-9]+) /?q=node/15&foobar=$1

Then in the custom php code for node/15 (aka foobar/), I get the argument with:
$foobar = $_GET["foobar"]

Thanks for the help

cog.rusty’s picture

Heh, brilliant! Sometimes things seem simple after you have figured them out.

jcoffland’s picture

I may be missing something here but I think you can do the following.

$path = split('/', $_GET['q']);
$num = $path[count($path) - 1];

This worked for me.

cog.rusty’s picture

Yes, the problem was before that.
Suppose you put this code in the "/foobar" page

If you go to a non-existent "/foobar/1" page, there is no code there to split the parts, just a "page not found".
If you go to the "/foobar" page where the code is, then the path has no useful parts.

jcoffland’s picture

I have a menu entry of type MENU_CALLBACK with path 'view'. When I load page http://www.example.com/view/1 it returns the page generated by my function at 'view' and I am able to parse the '/1' as a parameter using the code in my previous comment. I have clean URLs enabled. Also using drupal 5.2. This may be a special case but it works for me.

cog.rusty’s picture

Yes, this sounds better than Apache rewrites.
http://api.drupal.org/api/constant/MENU_CALLBACK/5

Is that menu_callback in a custom module?

nevets’s picture

If you are defining a menu callback, say view and use the the path view/1 you can define your callback as

function your_callback($your_argument) {
  ;
}

and the value of $your_argument will reflect the value of the next path component after view (in this case 1).
If you define the argument with a default value (ie $your_argument=some_default_value), you can still use just 'view' as a path.

In general, given a path and callback, the parts of the path not defined as part of the menu callback are available as arguments. So once again using view, if your path was view/1/2/3/4, you could define the function as

your_callback($arg1, $arg2, $arg3, $arg4)