The Browscap UserAgent Features AdapterOverviewBrowscap 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.
You may need to restart your webserver
The browscapphp.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:
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:
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.
getParam('bootstrap');
$userAgent = $bootstrap->getResource('useragent');
]]>
From your action controller, use getInvokeArg() to grab the
bootstrap, and from there, the user agent object.
getInvokeArg('bootstrap');
$userAgent = $bootstrap->getResource('useragent');
]]>
Within your view, you can grab it using the UserAgent view
helper.
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.
getDevice();
$cssSupport = $device->getFeature('cssversion');
$jsSupport = $device->getFeature('javascript');
switch (true) {
case ($jsSupport && $cssSupport >= 3):
$layout->setLayout('layout-html5');
break;
case ($jsSupport && $cssSupport < 3):
$layout->setLayout('layout-xhtml');
break;
case (!$jsSupport && $cssSupport < 3):
$layout->setLayout('layout-html-transitional');
break;
default:
$layout->setLayout('layout-web-1');
break;
}
]]>Configuration Options
The browscap adapter has no configuration options.
Available MethodsgetFromRequestarray $request, array $config
Decompose the request in order to return an array of device capabilities.