I have been reading about pathauto in combination with either panels or views. I think I understand how to set this up but its not really helping me do exactly what I need for my custom modules that use custom tables.

I have set up some custom modules for clients, but when it came to passing $_GET values I always just used "dirty" URL.

Typically I will use hook_menu() to define a MENU_CALLBACK...

	$items['products'] = array(
	'title' => 'Products',
	'page callback' => 'show_products',
	'access arguments' => array('view products'),
	'type' => MENU_CALLBACK,
	);

... so www.client.com/products shows the products pages.

I then use $_GET values to populate page data - www.client.com/products?p=product-name

if(isset($_GET['p'])) { 
$productName = $_GET['p']; 
//get product model from db, view the product, etc.
}

What I would like is a friendly URL like this: http://www.client.com/products/product-name

Obviously this points to http://www.client.com/index.php?q=products/product-name

I would like it to point to http://www.client.com/index.php?q=products&p=product-name

Is there any easy way to do this that I am overlooking? I don't want to have to use nodes for everything and then some custom views/panels/etc stuff. It all seems really convoluted to me. I want to use my own custom tables, forms, etc that I am used to in custom module development.

Can I set something up using custom_url_rewrite_inbound() perhaps? I am not very familiar with mod_rewrite but this seems like something that could be done using this function, but I am confused.

Comments

nevets’s picture

Here is one approach, define your menu hook as

    $items['products'] = array(
    'title' => 'Products',
    'page callback' => 'show_products',
    'page arguments' => array(1),
    'access arguments' => array('view products'),
    'type' => MENU_CALLBACK,
    );

The callback as

function show_products($product_name) {
 ...
}

and use a path like products/product-name.

If product name is optional declare the callback as

function show_products($product_name = NULL) {
 ...
}
saltednut’s picture

Thanks! I totally overlooked page_arguments... wow. Really easy to implement. Here's a quick example of the working code in case anyone finds this thread someday....

// $Id: customProducts.module,v 1 Exp $
//hook permissions
function customProducts_perm() {
  return array(
    'view products',
  );
}
//hook menu
function customProducts_menu() {
$items = array();
	$items['products'] = array(
	'title' => 'Products',
	'page callback' => 'view_products',
	'page arguments' => array(0,1,2),
	'access arguments' => array('view products'),
	'type' => MENU_CALLBACK,
	);
  return $items;
}

//assumes $menu will always be defined, with two extra arguments 
function view_products($menu_hook, $product = NULL, $size = NULL){
		$content .= $menu_hook.'<br />'; //will always say 'products'
		$content .= $product.'<br />'; //first new argument
		$content .= $size.'<br />'; //second new argument
		return $content;
}

Example string: http://www.client.com/products/bart-simpson-shirt/x-large

Will return:
Products
bart-simpson-shirt
x-large

mossill’s picture

this is awesome

Anonymous’s picture

Was looking for the same thing. This might be a simple solution.


$url_variable = arg(0);
$url_variable_2 = arg(1);

Edit: read up on check_plain() and check_url() as well.