acasademont/wurfl-bundle
Laravel/PHP bundle integrating the WURFL PHP API for device detection. Use WURFL capabilities in your app to identify phones, tablets, browsers, and other user-agent details and tailor content or features accordingly.
Installation
composer require acasademont/wurfl-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Acasademont\WurflBundle\ACFWurflBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console acf-wurfl:install
Update config/packages/acf_wurfl.yaml with your WURFL API key and other settings.
First Use Case Inject the WURFL service in a controller or service:
use Acasademont\WurflBundle\Service\WurflService;
class DeviceController extends AbstractController
{
public function __construct(private WurflService $wurfl)
{
}
public function detect(Request $request)
{
$userAgent = $request->headers->get('User-Agent');
$deviceInfo = $this->wurfl->getDeviceInfo($userAgent);
return $this->json($deviceInfo);
}
}
Device Detection in Controllers
public function showContent(Request $request)
{
$deviceInfo = $this->wurfl->getDeviceInfo($request->headers->get('User-Agent'));
if ($deviceInfo['is_tablet']) {
return $this->render('tablet_view.html.twig');
}
return $this->render('default_view.html.twig');
}
Caching Responses Cache WURFL responses for performance (e.g., using Symfony Cache component):
$cacheKey = md5($userAgent);
$deviceInfo = $this->cache->get($cacheKey, function() use ($userAgent) {
return $this->wurfl->getDeviceInfo($userAgent);
});
Custom Capability Checks Extend the bundle to add custom logic:
$capabilities = $this->wurfl->getCapabilities($userAgent);
if ($capabilities['flash_lite'] && $capabilities['resolution_width'] > 1024) {
// High-res Flash Lite device
}
Integration with Symfony Events
Use the kernel.request event to auto-detect devices:
$eventDispatcher->addListener(KernelEvents::REQUEST, function (RequestEvent $event) {
$request = $event->getRequest();
$request->attributes->set('device_info', $this->wurfl->getDeviceInfo($request->headers->get('User-Agent')));
});
Bulk Processing Process multiple user agents (e.g., for analytics):
$userAgents = ['Mozilla/5.0...', 'AppleWebKit/...'];
$results = $this->wurfl->batchDetect($userAgents);
API Key Management
%env(WURFL_API_KEY)%) in acf_wurfl.yaml.wurfl:
api_key: '%env(WURFL_API_KEY)%'
Rate Limiting
monolog logs for unexpected spikes.User-Agent Parsing
User-Agent strings can break detection. Validate input:
$userAgent = $request->headers->get('User-Agent', '');
if (empty($userAgent)) {
throw new \RuntimeException('User-Agent header missing');
}
Bundle Compatibility
User-Agent header (e.g., proxy bundles).Capability Names
is_tablet vs Is_Tablet). Refer to the WURFL Capabilities List for exact names.Enable Verbose Logging
Add to config/packages/acf_wurfl.yaml:
wurfl:
debug: true
Logs will include raw WURFL responses and errors.
Test with Known User Agents Use hardcoded user agents for debugging:
$deviceInfo = $this->wurfl->getDeviceInfo('Mozilla/5.0 (Linux; Android 10; SM-A505FN) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Mobile Safari/537.36');
Check HTTP Status Codes WURFL API errors (e.g., 401 Unauthorized) may not bubble up. Wrap calls:
try {
$deviceInfo = $this->wurfl->getDeviceInfo($userAgent);
} catch (\Exception $e) {
$this->logger->error('WURFL API error: ' . $e->getMessage());
// Fallback logic
}
Custom Capability Mappings Override capability names in config:
wurfl:
custom_capabilities:
is_mobile: is_mobile_browser
device_brand: brand_name
Event Listeners Extend the bundle by subscribing to its events (if supported). Example:
// In a service
$eventDispatcher->addListener(AcfWurflEvents::DEVICE_DETECTED, function (DeviceDetectedEvent $event) {
$event->setData('custom_field', 'value');
});
Database Storage Store WURFL responses in a database for offline use:
$this->wurfl->saveDeviceInfo($userAgent, $deviceInfo);
$storedInfo = $this->wurfl->getStoredDeviceInfo($userAgent);
Fallback Strategies Implement fallback logic for offline modes:
if (!$this->wurfl->isOnline()) {
return $this->wurfl->getFallbackDeviceInfo($userAgent);
}
How can I help you explore Laravel packages today?