How to pass parameters when using mod_rewrite

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
oi_antz

How to pass parameters when using mod_rewrite

Post by oi_antz »

I am writing a category/catalog module for a system that uses mod_rewrite with $config['use_hierarchy'] = true, and wondering if CMSms has a parser that will take parameters from the url and translate to $_GET variables. I can make one myself, I've done it several times, just wondering if there is a standard way to do this for compliance.

Eg.
Traditional url might be:
domain.com/index.php?page=browse-products&productId=43

Pretty url might be:
domain.com/productId/43/browse-products.html

Is this functionality provided for already, or am I right to write my own uri interpreter?
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: How to pass parameters when using mod_rewrite

Post by Dr.CSS »

Well news and others do it so you may want to look thru them to see how...
timstl
New Member
New Member
Posts: 4
Joined: Tue Aug 05, 2008 8:30 pm

Re: How to pass parameters when using mod_rewrite

Post by timstl »

There are a couple ways that you *can* do it.

The best way is to use setparameters/registerroute. There are docs that pretty much explain it, and the Skeleton module has a good example. Here is an example of how I did it, though:

In whatever.module.php you have a SetParameters function.

Code: Select all

function SetParameters()
	{
	
		// create friendly URL paremter 'page'
		$this->CreateParameter('page', '', $this->lang('help_page'));
		$this->SetParameterType('page', CLEAN_STRING);
		$this->RegisterRoute('/page\/(?P<pg>[0-9]+)$/', array('action'=>'default'));
       }
Then, when you create link, you have to pass the friendly_url parameter in...

Code: Select all

$i = 1; // for example			
$pages .= $this->CreateFrontendLink(
			$id,
			$returnid,
			'default',
			$i,
			array('pg' => $i),
			'',
			'',
			'',
			$class,
			false,
			'page/'.$i.'/'
			);
So your link will show up as http://www.whatever.com/page/1/ . Because you've setup the parameter/route, CMS will know that 1 is the "pg" variable, and that it's supposed to call the "default" action.
Pierre M.

Re: How to pass parameters when using mod_rewrite

Post by Pierre M. »

mod_rewrite + parameters => may be QSA (query string append) in the Apache doc can help.
domain.com/shop/browse.html?id=43

Pierre M.
Post Reply

Return to “Developers Discussion”