Creating a System Plugin to augment JRouter
m |
(Using a System Plugin to override the default Router functionality) |
||
| Line 46: | Line 46: | ||
define('ROUTER_MODE_SKIP_RAW', -1); | define('ROUTER_MODE_SKIP_RAW', -1); | ||
class plgSystemMyRouterReplacor extends JPlugin { | class plgSystemMyRouterReplacor extends JPlugin { | ||
| − | |||
public function onAfterInitialize() | public function onAfterInitialize() | ||
{ | { | ||
global $app; | global $app; | ||
// should we use $app = JFactory::getApplication('site'); ? | // should we use $app = JFactory::getApplication('site'); ? | ||
| − | |||
// get the router | // get the router | ||
$router = $app->getRouter(); | $router = $app->getRouter(); | ||
| − | |||
// add my own rules | // add my own rules | ||
// create a callback array to call the replaceRoute method of this object | // create a callback array to call the replaceRoute method of this object | ||
| Line 60: | Line 57: | ||
// attach the callback to the router | // attach the callback to the router | ||
$router->attachBuildRule($myRouterCallback); | $router->attachBuildRule($myRouterCallback); | ||
| − | |||
} | } | ||
/** | /** | ||
| Line 70: | Line 66: | ||
public function replaceRoute ($router, $uri) | public function replaceRoute ($router, $uri) | ||
{ | { | ||
| − | |||
global $app; | global $app; | ||
// should we use $app = JFactory::getApplication('site'); ? | // should we use $app = JFactory::getApplication('site'); ? | ||
$vars = array(); | $vars = array(); | ||
| − | |||
// get the true router | // get the true router | ||
$siteRouter= $app->getRouter() | $siteRouter= $app->getRouter() | ||
| − | |||
// to avoid a recursion trap, we need to make sure that only | // to avoid a recursion trap, we need to make sure that only | ||
// the site router can call us! We could have removed our own | // the site router can call us! We could have removed our own | ||
| Line 91: | Line 84: | ||
return $vars; | return $vars; | ||
} | } | ||
| − | |||
// we still want to clone the router passed to us, not the true router | // we still want to clone the router passed to us, not the true router | ||
// since the rules might be different | // since the rules might be different | ||
$myRouter = clone $router; | $myRouter = clone $router; | ||
| − | |||
//now use the power of Joomla! to parse this uri! | //now use the power of Joomla! to parse this uri! | ||
$vars = $myRouter->parse($vars); | $vars = $myRouter->parse($vars); | ||
| − | |||
//what is the menu id? what is the airspeed velocity of an unladen swallow? | //what is the menu id? what is the airspeed velocity of an unladen swallow? | ||
$menuId = ? isset($vars['itemId']) $vars['itemId'] : 0; | $menuId = ? isset($vars['itemId']) $vars['itemId'] : 0; | ||
| − | |||
// maybe you want to check menu parameters? Please be smarter than this. | // maybe you want to check menu parameters? Please be smarter than this. | ||
if ($menuId == 67) { | if ($menuId == 67) { | ||
| − | |||
// change the option to be a different component. | // change the option to be a different component. | ||
$vars['option'] == 'my_custom_component'; | $vars['option'] == 'my_custom_component'; | ||
| − | } | + | } |
| − | + | ||
| − | + | ||
// $vars has both the merged values of what would have been calculated and our changes | // $vars has both the merged values of what would have been calculated and our changes | ||
// So force the siteRouter to skip more processing - no reason to process it twice! | // So force the siteRouter to skip more processing - no reason to process it twice! | ||
| Line 116: | Line 102: | ||
//$router->setMode(JROUTER_MODE_SEF); | //$router->setMode(JROUTER_MODE_SEF); | ||
// if the router mode is SEF - integer 1, then _parseSEFRoute will be called by JRouter | // if the router mode is SEF - integer 1, then _parseSEFRoute will be called by JRouter | ||
| − | |||
// if we set it to something which is NOT SEF or RAW, nothing is done to it | // if we set it to something which is NOT SEF or RAW, nothing is done to it | ||
// false or null would be a logical choice, | // false or null would be a logical choice, | ||
| Line 134: | Line 119: | ||
$router->setMode(ROUTER_MODE_SKIP_SEF); | $router->setMode(ROUTER_MODE_SKIP_SEF); | ||
} | } | ||
| − | |||
// return our custom variables | // return our custom variables | ||
return $vars; | return $vars; | ||
} | } | ||
| − | |||
} | } | ||
{{JVer|2.5}} | {{JVer|2.5}} | ||
{{license}} | {{license}} | ||
Revision as of 06:29, 10 April 2012
Please note that the content on this page is currently incomplete. Please treat it as a work in progress.
- This article was last edited by Garyamort (talk| contribs) 13 months ago. (Purge)
| This is a article which: needs review. You can help the Joomla! Documentation Wiki by contributing to it. More pages that need help similar to this one are here. If you feel the need is satistified, please remove this notice. While actively editing, consider adding {{inuse}} to reduce edit conflicts. |
The Joomla! Router can be modified with additional rules by using the attatchXRules methods. Ideally, this can be done from a system plugin so these rules can affect be used by others.
2 possibilities when adding rules would be that you want to augment the router functionality, or you want to replace it.
Augmentation
class plgSystemMyRouterAugmentor extends JPlugin {
public function onAfterInitialize()
{
global $app;
// should we use $app = JFactory::getApplication('site'); ?
// get the router $router = $app->getRouter();
// add my own rules // create a callback array to call the augmentRoute method of this object $augmentCallback = array($this, 'augmentRoute '); // attach the callback to the router $router->attachBuildRule($myRouterCallback);
}
/**
* @param JRouterSite &$router The Joomla Site Router
* @param JURI &$uri The URI to parse
*
* @return array $vars The array of processed URI variables
- /
public function augmentRoute ($router, $uri)
{
$vars = array();
// do something to the vars
$vars['augment'] = 'succeeded';
// in this case, now to any Joomla! component/module it will look like ?augment=succeeded was in the url
return $vars; }
}
Replacement
define('ROUTER_MODE_SKIP_SEF', 2); define('ROUTER_MODE_SKIP_RAW', -1); class plgSystemMyRouterReplacor extends JPlugin {
public function onAfterInitialize()
{
global $app;
// should we use $app = JFactory::getApplication('site'); ?
// get the router
$router = $app->getRouter();
// add my own rules
// create a callback array to call the replaceRoute method of this object
$augmentCallback = array($this, 'replaceRoute ');
// attach the callback to the router
$router->attachBuildRule($myRouterCallback);
}
/**
* @param JRouterSite &$router The Joomla Site Router
* @param JURI &$uri The URI to parse
*
* @return array $vars The array of processed URI variables
- /
public function replaceRoute ($router, $uri)
{
global $app;
// should we use $app = JFactory::getApplication('site'); ?
$vars = array();
// get the true router
$siteRouter= $app->getRouter()
// to avoid a recursion trap, we need to make sure that only // the site router can call us! We could have removed our own // rule the myRouter...but that would only work // inside our own method! If someone else is also // doing the same thing, we would have an ugly little // recursion where they call parse which calls us // and then we clone router and call parse which calls them // back, and forth and back and forth // friends don't let friends use recursion!
if (spl_object_hash($router) != spl_object_hash($siteRouter)) {
// oh no! Abort, abort!
return $vars; }
// we still want to clone the router passed to us, not the true router // since the rules might be different
$myRouter = clone $router; //now use the power of Joomla! to parse this uri! $vars = $myRouter->parse($vars); //what is the menu id? what is the airspeed velocity of an unladen swallow? $menuId = ? isset($vars['itemId']) $vars['itemId'] : 0;
// maybe you want to check menu parameters? Please be smarter than this.
if ($menuId == 67) {
// change the option to be a different component.
$vars['option'] == 'my_custom_component';
}
// $vars has both the merged values of what would have been calculated and our changes
// So force the siteRouter to skip more processing - no reason to process it twice!
//$router->setMode(JROUTER_MODE_RAW);
// if the router mode is RAW - integer 0, then _parseRawRoute will be called by JRouter
//$router->setMode(JROUTER_MODE_SEF);
// if the router mode is SEF - integer 1, then _parseSEFRoute will be called by JRouter
// if we set it to something which is NOT SEF or RAW, nothing is done to it
// false or null would be a logical choice,
//but since the comparison is == not === false, null and 0/RAW are the same
// therefore we defined our own constants.
// SEF/RAW is a binary condition of either a positive integer or not a positive integer[0]
// so we extend that so SKIP_SEF becomes 2 so the if clause will fail, but anything else
// counting on checking for if mode > 0 will work
// and SKIP_RAW is -1, so anything counting on checking mode <=0 will work
// if it is not 0 or 1, then we don't change it because something else
// already triggered the skip.
$mode = $router-getMode();
if ($mode == JROUTER_MODE_RAW) {
$router->setMode(ROUTER_MODE_SKIP_RAW);
}
if ($mode == JROUTER_MODE_SEF) {
$router->setMode(ROUTER_MODE_SKIP_SEF);
}
// return our custom variables
return $vars;
}
}