PhpRiot
Follow phpriot on Twitter
Sponsored Link
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

Quick Start

First, you will need to download the following:

  • The WURFL PHP API. This archive contains the most recent wurfl-latest.xml file and patches which constitute the actual WURFL database.

We suggest that you inflate this archive in your "library" directory. Inflating the archive will create a wurfl-php-1.1 directory.

library
|-- wurfl-php-1.1
|   |-- COPYING
|   |-- docs
|   |-- examples
|   |-- README
|   |-- tests
|   `-- WURFL

Next, create a data and cache directory for the WURFL database and related cache files; this should be done from your project root (the directory containing the application and library directories). When you do so, make sure the directory is at least writable by the web server user; the following makes it writable for all users.

mkdir -p data/wurfl/cache
chmod -R o+rwX data/wurfl/cache

Now, copy the WURFL data from the inflated archive into your data directory.

cp library/wurfl-php-1.1/tests/resources/wurfl-latest.zip data/wurfl/
cp library/wurfl-php-1.1/tests/resources/web_browsers_patch.xml data/wurfl/

Create a WURFL configuration file named application/configs/wurfl-config.php, with the following contents:


<?php
$resourcesDir            
dirname(__FILE__) . '/../../data/wurfl/';

$wurfl['main-file']      = $resourcesDir  'wurfl-latest.zip';
$wurfl['patches']        = array($resourcesDir 'web_browsers_patch.xml');

$persistence['provider'] = 'file';
$persistence['dir']      = $resourcesDir '/cache/';

$cache['provider']       = null;

$configuration['wurfl']       = $wurfl;
$configuration['persistence'] = $persistence;
$configuration['cache']       = $cache;

Finally, edit your application.ini to add the following lines to your [production] section:

resources.useragent.wurflapi.wurfl_api_version = "1.1"
resources.useragent.wurflapi.wurfl_lib_dir = APPLICATION_PATH "/../library/wurfl-php-1.1/WURFL/"
resources.useragent.wurflapi.wurfl_config_file = APPLICATION_PATH "/configs/wurfl-config.php"

Note

The trailing directory separator on the wurfl_lib_dir setting is important. The WURFL API does no normalization, and expects it to be there.

At this point, everything is setup. The first request (from a mobile device) will populate the WURFL cache by parsing the resources/wurfl.xml file, and as such may take up to a minute. After that, lookups will be quite fast, and each request will contain detailed information on the user agent.

You can access this information in a variety of ways. From within the MVC portion of your application, you can access it via the bootstrap. Within plugins, this is done by grabbing the bootstrap from the front controller.

<?php
$bootstrap 
Zend_Controller_Front::getInstance()->getParam('bootstrap');
$userAgent $bootstrap->getResource('useragent');

From your action controller, use getInvokeArg() to grab the bootstrap, and from there, the user agent object.

<?php
$bootstrap 
$this->getInvokeArg('bootstrap');
$userAgent $bootstrap->getResource('useragent');

Within your view, you can grab it using the UserAgent view helper.

<?php
$userAgent 
$this->userAgent();

Once you have the user agent object, you can query it for different capabilities. As one example, you may want to use an alternate layout script based on the user agent capabilities.

<?php
$width 
$userAgent->getDevice()->getPhysicalScreenWidth();
switch (
true) {
    case (
$width <= 128):
        
$layout->setLayout('layout-poor');
        break;
    case (
$width <= 176):
        
$layout->setLayout('layout-medium');
        break;
    case (
$width <= 240):
        
$layout->setLayout('layout-high');
        break;
    case (
$width <= 320):
        
$layout->setLayout('layout-ultra');
        break;
    default:
        
// use default
        
break;
}

Finally, each device will often have a large number of capabilities not immediately represented in the device interface. You can query these using the hasFeature() and getFeature() methods.

<?php
if ($userAgent->hasFeature('mp3') && $userAgent->getFeature('mp3')) {
    
// embed HTML5 audio tag...
}

Zend Framework