Prettyurls met CTLModulemaker

Nederlandse ondersteuning voor CMS Made Simple

Moderator: velden

Post Reply
vandamhans
New Member
New Member
Posts: 5
Joined: Mon Jan 10, 2011 3:50 pm

Prettyurls met CTLModulemaker

Post by vandamhans »

Ik wil graag "pretty urls" hebben in mijn met CTLModulemaker gemaakte module.

Volgens de FAQ (zie onder), moet ik dan aanpassingen maken in mijn Routes.php bestand.

Ik heb prettyurls aangezet in de config.php van CMSMS. De pagina's buiten de module hebben dan ook al pretty urls.

De URL naar de item detail page is nu index.php?mact=Ereaders,cntnt01,default,0&cntnt01what=Ereaders&cntnt01alias=BeBook_One&cntnt01returnid=59
Deze zou ik graag als index.php/Ereaders/BeBook_One/59 hebben.

De routes.php die ik nu heb (en niet werkt) is als volgt:

Code: Select all

<?php
		$hierarchies = array();
$routes = array(

array(	'rule' => '"index.php\/(?P<Ereaders>[^/]+)\/(?P<alias>[^/]+)\/(?P<returnid>[0-9]+)$"',
			'builder' => array(	'index.php' 	=> 'string',
								'what' 	=> 'param',
'alias' 	=> 'param',
								'returnid'	=> 'param'
							),
			'defparams' => array('action'=>'default', 'what'=>'Ereaders')
	)


);	
Dit is wat in de FAQ van CTLModulemaker staat:
In later versions, the module creation wizard will handle routes and hierarchies setup. Until then, you have to setup routes manually - but it's far from complicated. It's all done in the module_specific/Routes.php file.

Let's say I have a module with two entities, called "category" and "item" (with each item having a dropdown field in which a category is selected). I wish to have an url of the form mymodule/category/returnid when I look at a category, and mymodule/category/item/returnid when I look at an item.
Right now, the Routes.php file is almost empty, with only two empty variables. We won't use the hierarchies variable, but we will populate the routes variable. The routes variable should be an array of routes, each route being in turn an array with three keys: "rule", "builder" and (optionally) "defparams". So our variable will have this kind of structure:

Code: Select all

$routes = array(
			array(
					'rule' => string,
					'builder' => array,
					'defparams' => array
				)	
		);
The "rule" element is exactly what one would put in the RegisterRoute function used in other modules. Therefore, I won't explain it in details here. The reason we put it here is that the module will use it to register routes, but also to read them and to create pretty urls.
Likewise, the "defparams" is exactly like in the RegisterRoute function: it is an array of default parameters that are not in the url. Usually, it will be array("action"=>"default"). "builder" is an array of the different parts of the url, in the order in which they appear. The key is what should be shown, and the value is the type of what is show (either a parameter or a string).

In our example, we would probably setup the following route:

Code: Select all

$routes = array(

	array(	'rule' => '"mymodule\/(?P<category>[^/]+)\/(?P<item>[^/]+)\/(?P<returnid>[0-9]+)$"',
			'builder' => array(	'mymodule' 	=> 'string',
								'category' 	=> 'param',
								'item'		=> 'param',
								'returnid'	=> 'param'
							),
			'defparams' => array('action'=>'default', 'what'=>'item')
	),
	
	array(	'rule' => '"mymodule\/(?P<category>[^/]+)\/(?P<returnid>[0-9]+)$"',
			'builder' => array(	'mymodule' 	=> 'string',
								'category' 	=> 'param',
								'returnid'	=> 'param'
							),
			'defparams' => array('action'=>'default', 'what'=>'category')
	)
		
);	

Once you've understood this, the module should do the rest.

Of course, more rules might be needed. In our example, pretty urls to items require that the category parameter be specified. If it isn't (like in search results), there's no pretty url. So we might want to also add and url without category, like:

Code: Select all

array(	'rule' => '"mymodule\/details\/(?P<item>[^/]+)\/(?P<returnid>[0-9]+)$"',
			'builder' => array(	'mymodule' 	=> 'string',
								'details' 	=> 'string',
								'item'		=> 'param',
								'returnid'	=> 'param'
							),
			'defparams' => array('action'=>'default', 'what'=>'item')
	)

And keep in mind that routes with less parameters should always be after those with more parameters.
Ik zou het erg waarderen als iemand van jullie kan zien wat ik fout doe!

Alvast bedankt,

Hans.
Post Reply

Return to “Dutch - Nederlands”