This is a quick proof-of-concept I put together after a discussion on how to deal with running a mobile site and a 'full' web site on the same hostname in a sane way, and to transparently route user agents to the appropriate site.
This way, any users accessing URLs beginning /mobile (or whichever path we nominate) will automatically be routed to the controllers in the Mobile module and users accessing URLs beginning /web will be routed into the Web module.
Users will be routed to this if they access any other path, such as / or /ringtones
The configuration for this in the bootstrap looks a little like this:
<?php
$frontController->setControllerDirectory(array(
'default' => '../application/modules/default/controllers',
'web' => '../application/modules/web/controllers',
'mobile' => '../application/modules/mobile/controllers',
));
That tells the FrontController where to look for the right controllers.
I chose to do this as a ControllerPlugin, as this will be run regardless of the user's entry URL.
<?php
require_once '/path/to/tera_wurfl/tera_wurfl.php';
class TwurflPlugin extends Zend_Controller_Plugin_Abstract
{
/**
* Only ever called once at the start of dispatch
* @access public
*/
public function dispatchLoopStartup($request)
{
$tw = new Tera_Wurfl();
$tw->getDeviceCapabilitiesFromAgent(
$request->getHeader('User-Agent'));
Zend_Registry::set('twurfl', $tw);
}
}
...and register that with the FrontController like so:
<?php
$frontController->registerPlugin(new TwurflPlugin());
A nice side effect is that the Tera_Wurfl object is pulled from the database once and once only, and is thereafter available via the Zend_Registry for the lifetime of the request.
<?php
class IndexController extends Zend_Controller_Action
{
/**
* Called automatically by ZF before a *Action()
* method is called
*
* @access public
*/
public function init()
{
$this->_helper->viewRenderer->setNoRender(TRUE);
}
/**
* Called by magic
*
* @access public
*/
public function __call($methodname, $args)
{
$tw = Zend_Registry::get('twurfl');
if ( $tw->browser_is_wap ) {
$this->_forward(
$this->_request->getActionName(),
$this->_request->getControllerName(),
'mobile');
//$this->_redirect('/mobile');
} else {
$this->_forward(
$this->_request->getActionName(),
$this->_request->getControllerName(),
'web');
}
}
}
Note the use of the PHP5 __call() magic method, which effectively gives us wildcarding of URLs, so we don't need to create an action method for every possible path.
Just wanted to drop you a line and let you know that Tera-WURFL 2.0 RC5 is out and the Stable 2.0.0 should be out before the end of the year. I have rewritten almost all of the code in a PHP5 OOP method and it is much more accurate and feature-filled. I changed the class name from "tera_wurfl" to "TeraWurfl", so you will need to make that change accordingly in your extension above.
Idel Fuschini