Arguments from URL into a node under cleanURLs?

dan
dan's picture
Joined: 04/16/2007
User offline. Last seen 3 years 28 weeks ago.

Starting with a URL of the form:

www.example.com/xyzzy/var1/var2

(where "xyzzy" refers to "node/x" through cleanURLs)

I'd like to be able to use the values of var1 & var2 in the PHP code for the node.

Can this be done?

(I'd rather not have to create an "xyzzy" module.)

I like cheese.

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
User offline. Last seen 21 weeks 6 days ago.
darn good question

That's a darn good question. The answer is yes you can, but it's not intuitive and does require a little coding. There is more than one way to do this.

There are a few things going on here to know about. When we are dealing with aliases realize the whole thing is an alias. When drupal gets the url it reads it to see if that is an alias. There are no arguments in an alias. The whole thing is an alias.

So, what we need to do is either write a custom module or we can intercept the url and do stuff to it to make it into arguments. Please note, this is for advanced users.

The funciton to check out is the custom_url_rewrite optional function you can put in your settings.php file. http://api.drupal.org/api/function/custom_url_rewr...

Here is an example use of the function...

<?php
function custom_url_rewrite($op, $result, $path, $path_language) {
  if (
$op == 'alias') {
   
//create an alias for the node
   
if ($path == 'node/x') {
     
$result = 'xyzzy/var1/var2';
    }
  }
  if (
$op == 'source') {
   
//read the result in and do something to it.

   
$url_explode = explode('/', $path);
    if (
$url_explode[0] == 'xyzzy') {
     
$result = 'node/x?';
      for (
$i = 1; $i <= count($url_explode); $i++) {
       
$result.= 'var'. $i .'='. $url_explode[$i] .'&';
      }
    }
  }

  return
$result;
?>

Granted, this is crude. And my code here is untested and a proof of concept for your idea. If you are going to do this in one or 2 places it would work. If you are going to use it often it might be better to build a simple module that provides this feature but scales better.

Hope this helps. Aliases really weren't meant for this so it's a bit of a push and bend.

Matt Farina
Geeks and God Co-Host
www.innovatingtomorrow.net
www.mattfarina.com

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

dan
dan's picture
Joined: 04/16/2007
User offline. Last seen 3 years 28 weeks ago.
Thanks so much!

I played around with this for awhile.

I eventually ended up going with a module instead, but this was helpful with other tasks.

I like cheese.