NOTE : If you want to contribute don't hesitate, I'll review any PR.
ZF2 Browscap is a Zend Framework 2 module that provides an improved get_browser function. This module could be standalone if "browscap" configuration setting in php.ini is undefined or does not point to the correct location of the browscap.ini file.
- Zend Framework 2 (latest master)
- Install browscap by cloning it into
./vendor/
. - Clone this project into your
./vendor/
directory.
-
Add this project in your composer.json:
"require": { "neilime/zf2-browscap": "dev-master" }
-
Now tell composer to download ZF2 Browscap by running the command:
$ php composer.phar update
-
Enabling it in your
application.config.php
file.<?php return array( 'modules' => array( // ... 'Neilime\Browscap', ), // ... );
ZF2 Browscap module provides a service, helper for views and plugin for controllers
-
Call Browscap with the service manager
/* @var $serviceManager \Zend\ServiceManager\ServiceLocatorInterface */ $browscap = $serviceManager->get('BrowscapService'); //Retrieve "\Neilime\Browscap\BrowscapService" object var_dump($browscap->getBrowser()); //Display an object which will contain various data elements representing, for instance, the browser's major and minor version numbers and ID string;
-
Call Browscap in a controller "get_browser" plugin expects the same params as the native php function get_browser.
$browscap = $this->get_browser(); //Retrieve an object echo $browscap->parent; $browscap = $this->get_browser(null,true); //Retrieve an array echo $browscap['parent']; $browscap = $this->get_browser( \Zend\Http\Header\UserAgent::fromString('User-Agent: Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19') ); //Retrieve an object with an arbitrary user agent echo $browscap->parent;
-
Call Browscap in a view "get_browser" helper expects the same params as the native php function get_browser.
$browscap = $this->get_browser(); //Retrieve an object echo $browscap->parent; $browscap = $this->get_browser(null,true); //Retrieve an array echo $browscap['parent']; $browscap = $this->get_browser( \Zend\Http\Header\UserAgent::fromString('User-Agent: Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19') ); //Retrieve an object with an arbitrary user agent echo $browscap->parent;
The default configuration is setup to use the native php function get_browser if it's available. Load browscap.ini from "http://browsers.garykeith.com/stream.asp?BrowsCapINI" and cache it in a file otherwise;
- boolean
allows_native_get_browser
: Define if the native php function get_browser could be used if it's available. - string
browscap_ini_path
: (optionnal) only needed if the native php function get_browser if it's unavailable or ifallows_native_get_browser
option is set to false. Define the borwscap.ini file path (could be an url) - Zend\Cache\Storage\Adapter|array
cache
: (optionnal) define the cache adapter. This not only saves you from loading and parsing the "browscap.ini" each time, but also guarantees an optimized loading procedure.
ZF2 Browscap provides console tools.
Load & parse "browscap.ini" file (or update cached browscap.ini)
php public/index.php load-browscap