The Browscap UserAgent Features Adapter

Overview

» Browscap is an open project dedicated to collecting an disseminating a "database" of browser capabilities -- actually a set of different files describing browser capablities. PHP has built-in support for using these files via the » get_browser() function. This function requires that your php.ini provides a browscap entry pointing to the PHP-specific php_browscap.ini file, which » you can download from the browscap site.

This class provides a features adapter that calls get_browser() in order to discover mobile device capabilities to inject into UserAgent device instances.

Note: You may need to restart your webserver
The browscap php.ini setting is a PHP_INI_SYSTEM setting, meaning it can only be set in your php.ini file or in your web server configuration. As such, you may need to restart your web server after adding the entry.

Quick Start

First, if you haven't already, » download the php_browscap.ini file, and put it somewhere your web server can access it; make sure the web server has permissions to read the file. Typically, you'll place this in the same location as your php.ini file.

Next, update your php.ini file to add the following line:

  1. browscap = /path/to/php_browscap.ini

Note: Keep it simple
If you put your php_browscap.ini file next to the php.ini file, you can omit the path information, and simply specify the filename.

Next, simply provide configuration to your application as follows:

  1. resources.useragent.mobile.features.classname = "Zend_Http_UserAgent_Device_Features_Browscap"

At this point, you're all set. You can access the browser 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.

  1. $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
  2. $userAgent = $bootstrap->getResource('useragent');

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

  1. $bootstrap = $this->getInvokeArg('bootstrap');
  2. $userAgent = $bootstrap->getResource('useragent');

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

  1. $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.

  1. $device = $userAgent->getDevice();
  2.  
  3. $cssSupport    = $device->getFeature('cssversion');
  4. $jsSupport     = $device->getFeature('javascript');
  5.  
  6. switch (true) {
  7.     case ($jsSupport && $cssSupport >= 3):
  8.         $layout->setLayout('layout-html5');
  9.         break;
  10.     case ($jsSupport && $cssSupport < 3):
  11.         $layout->setLayout('layout-xhtml');
  12.         break;
  13.     case (!$jsSupport && $cssSupport < 3):
  14.         $layout->setLayout('layout-html-transitional');
  15.         break;
  16.     default:
  17.         $layout->setLayout('layout-web-1');
  18.         break;
  19. }

Configuration Options

The browscap adapter has no configuration options.

Available Methods

getFromRequest ( array $request, array $config )

Decompose the request in order to return an array of device capabilities.

blog comments powered by Disqus