Mobile phones are taking over the world and, by extension, the web. Aside from the growth of responsive web design, websites are increasingly designing specific versions of their sites for mobile devices.
In this article we'll introduce MobileDetectBundle, which detects mobile devices
and helps you redirect users to the appropriate version of your website. Once
installed, this is the only configuration you need to setup the usual scenario
of redirecting mobile users to the m.
host of your website:
1 2 3 4 5 6 7 8
# app/config/config.yml
mobile_detect:
redirect:
mobile:
is_enabled: true
host: http://m.example.com
action: redirect
detect_tablet_as_mobile: true
Read the official documentation of this bundle to learn how to configure more complex redirection scenarios.
Apart from redirecting users, this bundle also provides a mobile_detector
service which allows you to detect device types, operating systems and even
vendors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$device = $this->get('mobile_detect.mobile_detector');
// basic device detection
$device->isMobile();
$device->isTablet();
// fine-grained device detection
$device->isIphone();
$device->isIpad();
$device->isSamsung();
// device operating system
$device->isIOS();
$device->isAndroidOS();
All these methods are also available in your Twig templates via the equivalent functions provided by the bundle:
1 2 3 4 5 6 7 8 9
{% extends is_mobile() ? 'mobile/layout.html.twig': 'layout.html.twig' %}
{% if is_android_os() %}
Download our application from the store.
{% endif %}
{% if is_device('samsung') %}
Thinking about buying an iPhone? Check out our deals!
{% endif %}
About the author
MobileDetectBundle is developed by Nikolay Ivlev, a Symfony developer from Moscow (Russia).
We're looking for the next bundle
Do you know any other useful Symfony bundle which is not well-known by the community yet? Tell us about it in the comments below or send us an email to javier.eguiluz@sensiolabs.com and we'll feature it in the next article of this series!
:) :) "if is_device('samsung') -> Thinking about buying an iPhone?" Mobile OS war in 3... 2... 1...
{% if is_device('samsung') %} Thinking about buying an iPhone? Check out our deals! {% endif %}
You make my day! :D
Ever since I came across this bundle, it has been the very first bundle I install in all of my Symfony Apps. Before I think of whether or not I want to create a community site, a blog, or anything else, I am always sure that I want a site that adopts to the specific features, needs and requirements of the browser and/or OS that is used to view it.
Looks nice, but is there any cookbook on using it for heavy-load sites behind caching proxy (e.g. Varnish)?
APYBreadcrumbTrailBundle is a nice bundle I use to manage breadcrumbs. It allows you to configure breadcrumbs through annotations for whole controller and in each action specifically. Also has Twig and PHP configuration. Before this, I had to manually define the breadcrumbs with an array in each action. Every time the route or the name of the item would change, I would have to trace every controller to make the changes. With this bundle, it's much more easier.
#Justinas Tamaševičius : For mobile detect with varnish, use https://github.com/varnish/varnish-devicedetect