|
Cookbook /
RouterSummary: Router allows a website's url structure to be different from PmWiki's group/page structure.
Version: 2007-12-19r12beta
Prerequisites: Tested with pmwiki-2.2.0-beta63. Should work with pmwiki-2.1.27
Status: beta
Maintainer: MagicBeanDip?
Download: router.phpΔ
Questions answered by this recipe
InstallationPlace router.phpΔ in your cookbook directory. Add the following lines near the top of your config.php file. $aRoute=array();
#Routing instructions go here
#$aRoute['PmWiki']='RoutedPmWiki';
include('cookbook/router.php');
Router is most useful with Usage InstructionsRouter adds the page variable $GroupUrl that returns a routed url to the base page of the current group. Instead of using Router expects to find an array of routing instructions in the global variable $aRoute. Routing instructions are processed first to last for inbound links and last to first for outbound links. Changes made by one instruction are passed on the the next instruction. Routing instructions can be specified in a short and long format Short Format: $aRoute['MatchPage']='MatchUrl'; Long Format: $aRoute[]=array(
'Mode'=>'BeginsWith'
,'PageName'=>'MatchPage'
,'Url'=>'MatchUrl'
);
Both instructions do the same thing. For an outbound link, they look for a page name that begins with "MatchPage" and substitute "MatchUrl" when a match is found.
This instruction is symetrical, so the reverse is true for an outbound link. The short instruction form only uses the "BeginsWith" mode for matching. Modes that currently exist are:
Symetrical Examples:Contains$aRoute[]=array( 'Mode'=>'Contains' ,'PageName'=>'MatchPage' ,'Url'=>'MatchUrl' );
EndsWith$aRoute[]=array( 'Mode'=>'EndsWith' ,'PageName'=>'MatchPage' ,'Url'=>'MatchUrl' );
ExactMatch$aRoute[]=array( 'Mode'=>'ExactMatch' ,'PageName'=>'MatchPageGroup/MatchPagePage' ,'Url'=>'MatchUrlGroup/MatchUrlPage' );
Also $aRoute[]=array( 'Mode'=>'ExactMatch' ,'PageName'=>'Main/HomePage' ,'Url'=>'' );
But more interestingly, Asymetrical ModesAsymetrical modes only work in one direction. The direction to apply the routing instruction to is specified by adding another element. ,'Direction'=>'Out'The 'Direction' element can be used with any instruction mode. RegReplace Example$aRoute[]=array( 'Mode'=>'RegReplace' ,'Search'=>'/^MatchUrl/' ,'Replace'=>'MatchPage' ,'Direction'=>'In' ); For inbound links,
But this instruction is limited to only inbound links, it is skipped when translating pagenames to urls for outbound links.
So a related outbound instruction is needed. $aRoute[]=array( 'Mode'=>'RegReplace' ,'Search'=>'/^MatchPage/' ,'Replace'=>'MatchUrl' ,'Direction'=>'Out' ); Now The RegReplace instruction simply passes the values of 'Search' and 'Replace' to the php preg_replace() function. If you specify a 'Limit' element in the routing instruction, that value will also be passed to preg_replace(). Special ModesSpecial modes mess with urls in an extra special way. :) CamelCaseToLowerCase ExampleThe 'CamelCaseToLowerCase' mode changes the entire pmwiki url structure from CamelCaseUrls to dashed-lower-case-urls. It functions in both directions unless you specifically add a 'Direction' element to the routing instruction. $aRoute[]=array( 'Mode'=>'CamelCaseToLowerCase' );
and vice versa When using CamelCaseToLowerCase in conjunction with the Apache mod-rewrite module, be sure to change your .htaccess to send all urls that start with lower case characters to index.php. Create your own routing modesAdd a function named The following function creates a routing mode named "Redirect" $aRoute[]=array(
'Mode'=>'Redirect'
,'In'=>'Main/HomePage'
,'Out'=>'http://example.com/'
);
///
/// Redirect to specified location when path matches
///
function RT_Redirect($path,$aline) {
if ('In'!=$aline['Direction']) {
return $path;
}
if ($path==@$aline['In']) {
header('Location: '.@$aline['Out'],TRUE,301);
}
return $path;
}
Release Notes
Comments |