Thanks for your reply.
Processed: raw content parsed by MODx, ready for output.
Handling the request: processes the original request, regardless of forwarding. MODx parses and outputs the requested resource as if it hadn’t been forwarded.
No matter, I got it to work, but I’d rather have a less hackish solution. It seems the main issue was the event that the plugin was associated with. Instead of OnHandleRequest (as you suggested), I used OnWebPageInit. This event gets fired just after the resource identifier is set in modRequest. Here’s the rest of what I did:
Plugin:
<?php
/*
* modMobile plugin
*
* Package by: Jeroen Kenters / www.kenters.com
* Based on code of Danny Herran (http://www.dannyherran.com/2011/02/detect-mobile-browseruser-agent-with-php-ipad-iphone-blackberry-and-others/)
*
* License: GNU GENERAL PUBLIC LICENSE Version 2, June 1991
*
* */
//get mobile theme setting
$mobile_template = $modx->getOption('mobile_template');
//only proceed when there is a mobile theme
if($mobile_template)
{
$_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
$mobile_browser = '0'; //default: not mobile
if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
$mobile_browser++;
if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))
$mobile_browser++;
if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
$mobile_browser++;
if(isset($_SERVER['HTTP_PROFILE']))
$mobile_browser++;
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda','xda-'
);
if(in_array($mobile_ua, $mobile_agents))
$mobile_browser++;
if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)
$mobile_browser++;
if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'ipad') !== false)
$mobile_browser++;
if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)
$mobile_browser=0;
//change template if mobile browser is found
if($mobile_browser>0) {
//$modx->resource->set('template', $mobile_template);
//$modx->resource->set('context_key', 'mobile');
$_GET['mResourceMethod'] = $modx->request->modx->resourceMethod;
$_GET['mResourceIdentifier'] = $modx->request->modx->resourceIdentifier;
$modx->sendForward(27);
}
}
return true;
Snippet (called from the resource the request gets forwarded to):
//get resource we're requesting
$mResourceMethod = $_GET['mResourceMethod'];
$mResourceIdentifier = $_GET['mResourceIdentifier'];
$mResource = $modx->getObject('modResource',($mResourceMethod == 'id' ? $mResourceIdentifier : array($mResourceMethod => $mResourceIdentifier)));
//gets resource content
$mResourceContent = $mResource->getContent();
//output retrieved resource
echo $mResourceContent;
Also, the resource that receives the forwarded request is uncached and has the template set to the mobile template.
This works, but I’d rather have it done right. Caching would be nice, and my first thought was to simply choose a different cache path (along with the template switch) when a user with a mobile device visits the site. This would allow for caching, and be very simple. Do you have any ideas?