Managing Mobile and Non-mobile Versions of a Site Using Tera-WURFL and Zend Framework

Posted in Mobile, PHP, Programming and Zend Framework on Tuesday, the 12th of February, 2008.

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.

Steps Involved

i) Organise the 'Web' and 'Mobile' sites as separate Modules in Zend Framework

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.

ii) Add a 'Default' 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.

iii) Query Tera-WURFL to identify the device

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.

iv) Use the IndexController of the Default module to route requests into the appropriate module

<?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.

Outcome

Comments

Posted by Idel Fuschini on Tuesday, the 1st of September, 2009.

Hi,
I'm Idel Fuschini and I have created Apache Mobile Filter that allows you to access WURFL from any programming language, not just Java and php that is traditionally used for dynamic mobile web sites.

The module detects the mobile device and passes the WURFL capabilities on to the other web application as environment variables. It can also be used to resize images on the fly to adapt to the screen size of the mobile device.

Let me know your opinion.

For more info: http://www.idelfuschini.it/en/apache-mobile-filter-v2x.html
Mobile Test Page: http://apachemobilefilter.nogoogle.it/

Posted by Steve Kamerman on Thursday, the 5th of November, 2009.

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.

Posted by Idel Fuschini on Monday, the 30th of August, 2010.

Hi,
just some news, now there is a new site for apache mobile filter where it's possible to test the software.
The site is: www.apachemobilefilter.org

Let me know your opinion

Enter your comment: