12 Feb 2008, 10:06 p.m.

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

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

  • Users can, should they wish, access each site from any device, by explicitly browsing to the relevant URL
  • Users not specifying /mobile or /web will be detected and routed to the correct site
  • This does not require any browser redirects - it's transparent to the end user
  • URLs such as /ringtones or /sendtoafriend will work transparently allowing the appropriate controllers to handle them as they see fit
  • For such time as the mobile site is not Zend Framework based, we can replace the forward() with a redirect() as per the line commented out in the previous example
Posted by Simon in PHP, Zend Framework, Mobile and Programming
1 Sep 2009, 3:03 p.m.

Idel Fuschini

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/

5 Nov 2009, 1:31 a.m.

Steve Kamerman

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.

30 Aug 2010, 11:44 p.m.

Idel Fuschini

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