david221189av/mobile-detect-bundle
Installation:
composer require tattali/mobile-detect-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Tattali\MobileDetectBundle\TattaliMobileDetectBundle::class => ['all' => true],
];
Configuration: Publish the default config:
php bin/console tattali:mobile-detect:install
Edit config/packages/tattali_mobile_detect.yaml to define:
mobile_route: '/mobile', desktop_route: '/desktop').excluded_routes: ['/admin']).tablet_user_agent_patterns).First Use Case: Trigger detection in a controller:
use Tattali\MobileDetectBundle\Service\MobileDetectService;
class HomeController extends AbstractController
{
public function index(MobileDetectService $detectService)
{
if ($detectService->isMobile()) {
return $this->redirectToRoute('mobile_home');
}
// Desktop logic...
}
}
Route-Based Redirection:
Use the MobileDetectService to redirect users automatically:
# config/routes.yaml
mobile_home:
path: /mobile
controller: App\Controller\MobileController::index
// Controller
public function index(MobileDetectService $detectService)
{
if ($detectService->isMobile()) {
return $this->redirectToRoute('mobile_home');
}
return $this->render('desktop/home.html.twig');
}
Twig Integration: Pass the service to templates for conditional rendering:
{% if mobileDetect.isMobile() %}
{{ include('mobile/_header.html.twig') }}
{% else %}
{{ include('desktop/_header.html.twig') }}
{% endif %}
Register the service in Twig:
# config/packages/twig.yaml
twig:
globals:
mobileDetect: '@tattali_mobile_detect.mobile_detect'
Dynamic Route Switching:
Override routes in MobileDetectService for complex logic:
$detectService->setMobileRoute('/custom-mobile-path');
$detectService->setDesktopRoute('/custom-desktop-path');
Event-Based Detection:
Listen for mobile.detect events to customize behavior:
// src/EventListener/MobileDetectListener.php
class MobileDetectListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
MobileDetectEvents::DETECT => 'onMobileDetect',
];
}
public function onMobileDetect(MobileDetectEvent $event)
{
if ($event->isMobile() && $event->getUserAgent()->isTablet()) {
$event->setMobileRoute('tablet_specific_route');
}
}
}
User-Agent Overrides:
if ($detectService->isMobile() && !$detectService->isBot()) {
// Proceed with mobile logic
}
Caching Headers:
User-Agent. Ensure caching headers (e.g., Cache-Control) don’t interfere:
# config/packages/framework.yaml
http_client:
default_options:
headers:
'Cache-Control': 'no-cache'
Route Exclusions:
excluded_routes may break expected behavior. Test with:
php bin/console debug:router | grep excluded
Tablet Detection:
tattali_mobile_detect:
tablet_user_agent_patterns:
- 'iPad'
- 'Android.*Tablet'
- 'Kindle'
Log User Agents: Add a debug endpoint to inspect requests:
// src/Controller/DebugController.php
class DebugController extends AbstractController
{
public function userAgent(MobileDetectService $detectService)
{
return $this->json([
'isMobile' => $detectService->isMobile(),
'isTablet' => $detectService->isTablet(),
'userAgent' => $detectService->getUserAgent()->getUserAgentString(),
]);
}
}
Event Debugging:
Dump MobileDetectEvent data in a subscriber:
public function onMobileDetect(MobileDetectEvent $event)
{
file_put_contents(
'debug/mobile_detect.log',
print_r($event->toArray(), true)
);
}
Custom Detection Logic:
Extend the MobileDetectService:
// src/Service/CustomMobileDetectService.php
class CustomMobileDetectService extends MobileDetectService
{
public function isMobile()
{
if ($this->getUserAgent()->is('iPhone') && $this->isSmallScreen()) {
return true;
}
return parent::isMobile();
}
private function isSmallScreen(): bool
{
// Custom logic (e.g., check for viewport width)
}
}
Register as a service:
services:
tattali_mobile_detect.mobile_detect:
class: App\Service\CustomMobileDetectService
arguments: ['@request_stack']
Database-Backed Device Rules: Store device patterns in a database and hydrate the service:
// src/Service/DatabaseMobileDetectService.php
class DatabaseMobileDetectService extends MobileDetectService
{
public function __construct(
RequestStack $requestStack,
EntityManagerInterface $em
) {
$this->em = $em;
parent::__construct($requestStack);
}
public function isMobile(): bool
{
$patterns = $this->em->getRepository(DevicePattern::class)
->findBy(['type' => 'mobile']);
$this->setUserAgentPatterns($patterns);
return parent::isMobile();
}
}
Geolocation Hybrid Detection:
Combine with geolocation services (e.g., geoip2/geoip2) for region-specific mobile handling:
public function isMobile(): bool
{
$isMobile = parent::isMobile();
$geo = $this->geoService->getLocation();
if ($isMobile && $geo->getCountry()->getIsoCode() === 'US') {
return $this->isUsMobileDevice();
}
return $isMobile;
}
How can I help you explore Laravel packages today?