suncat/mobile-detect-bundle
Symfony bundle integrating Mobile_Detect to identify phones/tablets by user agent, manage device-specific views (mobile/tablet/full), and optionally redirect users to mobile or tablet versions of your site. Supports Symfony 2.4–4.0.
Installation
composer require suncat/mobile-detect-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Suncat\MobileDetectBundle\SuncatMobileDetectBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console config:dump-reference SuncatMobileDetectBundle
Or override in config/packages/suncat_mobile_detect.yaml:
suncat_mobile_detect:
mobile_view: 'mobile' # 'mobile', 'tablet', or 'full'
redirect:
mobile: ~ # URL or false to disable
tablet: ~ # URL or false to disable
user_agent: ~ # Custom User-Agent string (optional)
First Use Case Detect device type in a controller:
use Suncat\MobileDetectBundle\MobileDetect;
public function index(MobileDetect $detect)
{
$deviceType = $detect->getDeviceType(); // 'mobile', 'tablet', or 'desktop'
$isMobile = $detect->isMobile();
$isTablet = $detect->isTablet();
return $this->render("page/{$deviceType}_view.html.twig");
}
Device Detection in Controllers
Inject MobileDetect service to dynamically adjust responses:
public function show(MobileDetect $detect, Request $request)
{
if ($detect->isMobile() && $detect->is('iPhone')) {
return $this->redirectToRoute('mobile_optimized_route');
}
return $this->render('default_template.html.twig');
}
Twig Integration
Use the mobile_detect Twig extension:
{% if mobile_detect.isMobile() %}
<link rel="stylesheet" href="{{ asset('css/mobile.css') }}">
{% endif %}
Redirect Management
Configure redirects in config/packages/suncat_mobile_detect.yaml:
redirect:
mobile: 'https://m.example.com'
tablet: 'https://tablet.example.com'
Trigger redirects via event listener (optional):
use Suncat\MobileDetectBundle\Event\MobileDetectEvent;
public function onMobileDetect(MobileDetectEvent $event)
{
if ($event->isMobile() && $event->getRedirectUrl()) {
$event->setRedirect(true);
}
}
Custom Device Checks Extend detection logic:
$detect->is('iPad'); // Checks for iPad specifically
$detect->is('Android'); // Checks for Android OS
$detect->isTablet(); // Checks for any tablet
RequestContext to dynamically set _format or _locale based on device type.MobileDetect in PHPUnit:
$detect = $this->createMock(MobileDetect::class);
$detect->method('isMobile')->willReturn(true);
$this->container->set('mobile_detect', $detect);
User-Agent Overrides
User-Agent header matches the device you’re testing. Use browser extensions (e.g., "User-Agent Switcher") or tools like curl:
curl -A "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2 like Mac OS X)" http://example.com
Redirect Loops
redirect: { mobile: false, tablet: false }) to avoid infinite loops when testing.False Positives/Negatives
is() for granular checks:
if ($detect->is('Android') && !$detect->is('Tablet')) {
// Handle Android phones specifically
}
Configuration Conflicts
mobile_view and redirect settings align with your routing (e.g., if mobile_view: 'mobile' but no mobile route exists, requests will 404).Log Device Info Dump detection results for debugging:
$detect->getUserAgent();
$detect->getDeviceName();
$detect->getOsName();
$detect->getVersion();
Check Events
Listen to mobile_detect events to debug redirect logic:
# config/services.yaml
Suncat\MobileDetectBundle\EventListener\MobileDetectListener:
tags:
- { name: kernel.event_listener, event: mobile_detect, method: onMobileDetect }
Custom Device Rules
Extend the MobileDetect class or override the service:
# config/services.yaml
Suncat\MobileDetectBundle\MobileDetect:
arguments:
- '@service_container' # Pass custom dependencies
calls:
- [setCustomRules, ['%kernel.project_dir%/config/mobile_rules.php']]
Event Subscribers
Subscribe to mobile_detect events for custom logic:
use Suncat\MobileDetectBundle\Event\MobileDetectEvent;
class CustomMobileSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
MobileDetectEvent::NAME => 'onMobileDetect',
];
}
public function onMobileDetect(MobileDetectEvent $event)
{
if ($event->isMobile() && $event->getDeviceName() === 'iPhone') {
$event->setData(['ios_version' => $event->getVersion()]);
}
}
}
Override Templates
Create device-specific templates in templates/page/:
templates/
page/
mobile_view.html.twig
tablet_view.html.twig
full_view.html.twig
is() are case-sensitive (e.g., is('iPhone') vs. is('iphone')).mobile_view is not set, defaults to 'full' (desktop view).mobile_view if both are configured.Request object if reused.Mobile_Detect library is optimized, but avoid calling is() repeatedly in loops.How can I help you explore Laravel packages today?