views:

277

answers:

3

I have a drupal page (content type page) that should accept GET values using clean urls.

I put the following code in the page body using the php code input format.

<?php
  $uid = $_GET['uid'];
  $user = user_load($uid);
  print $user->name;
?>

Now the following link http://www.example.com/mypath/?uid=5 results in user 5's name being displayed. Great.

My question is: Can this be accomplished using clean urls such that going to http://www.example.com/mypath/5 has the same result? (Similar to using arguments in views)

+2  A: 

You can do what you ask in the menu system using hook_menu in a module, but you can't create an alias where a part of it is a name. The whole alias is a name, so you can't extract info from it. Using hook_menu you can do this:

function my_module_menu() {
  $items = array();

  $items['path/%user'] = array(
    'title' => 'Title',
    'page callback' => 'callback',
    'page arguments' => array(1),
    'access callback' => '...',
    'access arguments' => array(...),
  );

  return $items;
}

Then in your callback function you will have the value or argument 1 in the path which corresponds to the uid (in reality it will be a loaded user object because of %user).

googletorp
A: 

If you use Views to assemble these pages, that part can be done easily for you by putting UID as an argument provided in the URL.

Kevin
+2  A: 

Yes. Use arg() instead of $_GET

Keep in mind that arg() uses the $_GET['q'] by default and so effectively translates an alias before returning the result. Thus, the parameter you are trying to access may be in another position.

For example, the path myalias/5 might translate into taxonomy/term/5. In this case, arg(2) is 5.

For more information on the Drupal arg() function, see http://api.drupal.org/api/function/arg/6

Markus Sandy
Good idea. However, http://api.drupal.org/api/function/arg/6 notes that using arg() is less readable than a menu callback as suggested by googletorp. I'll try both ways and see which works best.
calebthorne