Detect mobile devices with the WURFL API

by Pierre
This component uses the WURFL API to detect any mobile or wireless device, and sets custom paths for your views and css, so you can easily create a mobile version of your website.
This component uses the WURFL API to detect any mobile or wireless device, and sets custom paths for your views and css, so you can easily create a mobile version of your website.

Lets get started.

The WURFL API


First of all, download the WURFL API package here (Make sure to download the 1.1 version).

Extract the entire WURFL folder in the package to your /app/vendors folder.
Create a directory /app/configs/wurfl, and extract everything in the resources folder to that directory.

In the examples/resources directory, extract the file in the wurfl-regression.zip to your /app/configs/wurfl directory.
Edit the wurfl-config.xml file to look like the following:

<?xml version="1.0" encoding="UTF-8"?>
<wurfl-config>
    <wurfl>
        <main-file>wurfl.xml</main-file>
        <patches>
            <patch>web_browsers_patch.xml</patch>
        </patches>
    </wurfl>
    <persistence>
        <provider>file</provider>
        <params>dir=../../tmp/cache/wurfl</params>
    </persistence>
    <cache>
        <provider>null</provider>
    </cache>
</wurfl-config>

Create the folder /app/tmp/cache/wurfl.

The Component


Create a file /app/controllers/components/mobiledetect.php and put the following in the file:

Component Class:

<?php 
class MobiledetectComponent extends Object
{
    var 
$isMobile false;

    function 
startup(&$controller)
    {
        
$this->controller =& $controller;
    }

    function 
detect()
    {
        
        
App::import'Vendor''WURFL', array( 'file' => 'WURFLManagerProvider.php')); 
        
        
$wurflConfigFile CONFIGS.'wurfl/wurfl-config.xml';

        
$wurflManager WURFL_WURFLManagerProvider::getWURFLManager($wurflConfigFile);
        
        
$requestingDevice $wurflManager->getDeviceForHttpRequest($_SERVER);

        if(
$requestingDevice->getCapability('is_wireless_device') == 'true')
        {
                        
$this->isMobile true;
            
$this->setMobile();
                        
$this->controller->theme 'mobile';
                  
$this->controller->layoutPath 'mobile';
        }
        
       
    }
}
?>

In your app_controller, remember to include the component like this:

var $components = array('Mobiledetect');

and add to the beforeFilter function the following:

function beforeFilter()
{
    $this->Mobiledetect->startup($this);
    $this->Mobiledetect->detect();
}

And thats it! Now you can create a custom layout file for your mobile website, located under /app/views/layouts/mobile.
You also need to put your css in a directory in /app/webroot/themed/mobile/css
and (optional)images in /app/webroot/themed/mobile/css/img.
Your views need to go to /app/views/themed/mobile.

Your done! Now you can create a mobile version of your website.

Report

More on Components

Advertising

Comments

  • scabadaska posted on 09/11/11 01:56:26 PM
    This article is helpful but a bit out of date now with the 1.3 PHP version of WURFL. Anyone trying to use this can achieve something similar with this code in the component:

    function detect()
    {

    App::import( 'Vendor', 'WURFL', array( 'file' => 'Application.php'));

    $wurflConfigFile = CONFIGS.'wurfl/wurfl-config.xml';
    $wurflConfig = new WURFL_Configuration_XmlConfig($wurflConfigFile);

    $wurflManagerFactory = new WURFL_WURFLManagerFactory($wurflConfig);

    $wurflManager = $wurflManagerFactory->create();
    $wurflInfo = $wurflManager->getWURFLInfo();

    $device = $wurflManager->getDeviceForHttpRequest($_SERVER);

    debug($device->getAllCapabilities());

    }
  • amfriedman posted on 05/27/11 08:19:21 PM
    Just use this extremely simple web service instead. It houses the WURFL library. You just make an HTTP call and get the JSON results. Easy peasy.
    http://www.wurflws.com/
  • twinreal posted on 09/13/10 01:43:58 PM
    I'm getting these error and have not idea what is it.

    Fatal error: Uncaught exception 'WURFL_WURFLException' with message'Device with device id generic_android is not defined.' in D:\Apache\mysoft\www\vendors\WURFL\DeviceRepository.php:51 Stack trace: #0 D:\Apache\mysoft\www\vendors\WURFL\WURFLService.php(95): WURFL_DeviceRepository->getDevice('generic_android') #1 D:\Apache\mysoft\www\vendors\WURFL\WURFLService.php(47): WURFL_WURFLService->getWrappedDevice('generic_android') #2 D:\Apache\mysoft\www\vendors\WURFL\WURFLManager.php(42): WURFL_WURFLService- >getDeviceForRequest(Object(WURFL_Request_GenericRequest)) #3 D:\Apache\mysoft\www\vendors\WURFL\WURFLManager.php(57): WURFL_WURFLManager- >getDeviceForRequest(Object(WURFL_Request_GenericRequest)) #4 D:\Apache\mysoft\www\controllers\components\mobiledetect.php(20): WURFL_WURFLManager->getDeviceForHttpRequest(Array) #5 D:\Apache\mysoft\www\controllers\links_controller.php(13): MobiledetectComponent->detect() #6 D:\Apache\cakecore\cake\dispatcher.php(209): BacklinksController->befo in D:\Apache\mysoft\www\vendors\WURFL\DeviceRepository.php on line 51
  • eimermusic posted on 05/12/10 03:15:49 AM
    Android is a pretty new device family. To get support for it and other new devices just update the WURFL database regularly.
  • Dankroad posted on 05/03/10 02:46:27 PM
    Can the author respond to the first comment? It doesn't detect the Android phone - I would think that would a showstopper for most people, including myself.

    It doesn't seem like anybody has actually tested this code, because the component code that is posted doesn't work out of the box. First the call $this->setMobile() is an illegal call because that function doesn't even exist. Second you need to set the view variable of the controller in order to use the new mobile theme. So in the if clause in the detect() function insert this:

     $this->controller->view = 'Theme';

    That should get you up and running. Now on to the vendor code. The latest release 1.1/1.r2 is broken - I don't know why it's posted. It died on a missing argument error. The 1.1/1.1r release works, but is about a year old now - is that why it doesn't know about android?? I noticed the wurfl.xml file in 1.r1 is much smaller than the wurfl.xml file in 1.r2. Can I take the one from 1.r2 and drop it in the 1.r1 file structure?

    Does anybody know of a better wireless detection php vendor?
  • eimermusic posted on 03/15/10 10:29:13 AM
    Nice basic wurfl setup for Cake. But that is a lot of code for simply detecting a mobile device. Wurfl has so much more to offer and I would have liked to see it put to more use.

    People tickled by mobile detection should do this:

    debug($requestingDevice->getAllCapabilities());

    That will print almost 500 pieces of information about the device. (logging it is probably better)

    To get the benefits worth the "cost" of a heavy data-set like the wurfl database (current version is 14,5MB) you probably want to optimize things like images, video conversions, applications types or any other "content" presented to the mobile browser.

    When fully leveraged, Wurfl is really powerful.

    Btw: The "real" database is located and updated at: http://wurfl.sourceforge.net/ The one mentioned in the article is a limited test database. Remember to empty the cache after updating the wurfl xml.
  • stringtokenizer posted on 03/14/10 10:47:24 PM
    Fatal error: Uncaught exception 'WURFL_WURFLException' with message'Device with device id generic_android is not defined.' in D:\Apache\mysoft\www\vendors\WURFL\DeviceRepository.php:51 Stack trace: #0 D:\Apache\mysoft\www\vendors\WURFL\WURFLService.php(95): WURFL_DeviceRepository->getDevice('generic_android') #1 D:\Apache\mysoft\www\vendors\WURFL\WURFLService.php(47): WURFL_WURFLService->getWrappedDevice('generic_android') #2 D:\Apache\mysoft\www\vendors\WURFL\WURFLManager.php(42): WURFL_WURFLService- >getDeviceForRequest(Object(WURFL_Request_GenericRequest)) #3 D:\Apache\mysoft\www\vendors\WURFL\WURFLManager.php(57): WURFL_WURFLManager- >getDeviceForRequest(Object(WURFL_Request_GenericRequest)) #4 D:\Apache\mysoft\www\controllers\components\mobiledetect.php(20): WURFL_WURFLManager->getDeviceForHttpRequest(Array) #5 D:\Apache\mysoft\www\controllers\links_controller.php(13): MobiledetectComponent->detect() #6 D:\Apache\cakecore\cake\dispatcher.php(209): BacklinksController->befo in D:\Apache\mysoft\www\vendors\WURFL\DeviceRepository.php on line 51
    • aress posted on 11/21/10 10:39:31 PM
      Hello,

      I have install all the setup as per given instructions i am getting error message while executing my application.
      Here is message on my local machine
      Fatal error: Class 'WURFL_WURFLManagerProvider' not found in C:\wamp\www\projects\mobilemessanger\app\controllers\components\mobiledetect.php on line 18

      Also there is no such file WURFLManagerProvider.php in the downloaded folder :(

      Here is instruction given.
      Add a require_once to 'WURFL_Installation/WURFL/WURFLManagerProvider.php';

      Thanks
login to post a comment.