Symfony2 bundle for detect mobile devices, managing mobile view types, redirect to mobile version.

Installs: 420 373

Dependents: 5

Stars: 257

Watchers: 32

Forks: 55

Open Issues: 8

Type: symfony-bundle

v0.10.3 2016-01-19 15:49 UTC


Symfony2 bundle for detect mobile devices, manage mobile view and redirect to the mobile and tablet version.

Build Status Latest Stable Version Total Downloads


This Bundle use Mobile_Detect class and provides the following features:

  • Detect the various mobile devices by Name, OS, browser User-Agent
  • Manages site views for the various mobile devices (mobile, tablet, full)
  • Redirects to mobile and tablet sites

Switch device view

For switch device view, use device_view GET parameter:{full/mobile/tablet}



Add to composer.json in your project to require section:

    "suncat/mobile-detect-bundle": "0.10.*"

Run command: php composer.phar install

Add this bundle to your application's kernel

public function registerBundles()
    return array(
         // ...
        new SunCat\MobileDetectBundle\MobileDetectBundle(),
        // ...

Configure service in your YAML configuration

        mobile: ~
        tablet: ~
    switch_device_view: ~

Full configuration

You can change default behaviour of your redirects with action parameter:

  • redirect: redirects to appropriate host with your current path
  • no_redirect: no redirection (default behaviour)
  • redirect_without_path: redirects to appropriate host index page
            is_enabled: true            # default false
            host:       # with scheme (http|https), default null, url validate
            status_code: 301            # default 302
            action: redirect            # redirect, no_redirect, redirect_without_path
            is_enabled: true            # default false
            host:     # with scheme (http|https), default null, url validate
            status_code: 301            # default 302
            action: redirect            # redirect, no_redirect, redirect_without_path
            is_enabled: true            # default false
            host:     # with scheme (http|https), default null, url validate
            status_code: 301            # default 302
            action: redirect            # redirect, no_redirect, redirect_without_path
        detect_tablet_as_mobile: true   # default false
        save_referer_path: false        # default true
                                        # true  redirectUrl =
                                        # false redirectUrl =
        mobile_detector: mobile_detect.mobile_detector.default
    device_view_class: SunCat\MobileDetectBundle\Helper\DeviceView
    request_listener_class: SunCat\MobileDetectBundle\EventListener\RequestListener
    extension_class: SunCat\MobileDetectBundle\Twig\Extension\MobileDetectExtension

You can also create route specific rules for redirecting in your routing.yml. Just add appropriate platform(s) to the options field and add a redirect rule.

    pattern:  /someaction
    defaults: { _controller: YourBundle:Index:someAction }
    options:  { mobile: redirect, tablet: no_redirect, full: redirect_without_path }         # redirect, no_redirect, redirect_without_path

PHP examples

Check type device

$mobileDetector = $this->get('mobile_detect.mobile_detector');

Check phone



Check tablet

is[BlackBerryTablet|iPad|Kindle|SamsungTablet|HTCtablet|MotorolaTablet|AsusTablet|NookTablet|AcerTablet| YarvikTablet|GenericTablet]


Check mobile OS



Check mobile browser User-Agent



Twig Helper

{% if is_mobile() %}
{% if is_tablet() %}
{% if is_device('iphone') %} # magic methods is[...]
{% if is_ios() %}
{% if is_android_os() %}
{% if is_full_view() %}
{% if is_mobile_view() %}
{% if is_tablet_view() %}
{% if is_not_mobile_view() %}
{{ full_view_url() }}

Twig examples

{% extends is_mobile() ? "MyBundle:Layout:mobile.html.twig" : "MyBundle:Layout:full.html.twig" %}
{% if is_mobile_view() %}
    {% extends "MyBundle:Layout:mobile.html.twig" %}
{% else if is_tablet_view() %}
    {% extends "MyBundle:Layout:tablet.html.twig" %}
{% else if is_full_view() or is_not_mobile_view() %}
    {% extends "MyBundle:Layout:full.html.twig" %}
{% endif %}
{% if is_device('iphone') %}
    <link rel="stylesheet" href="{{ asset('css/iphone.css') }}" type="text/css" />
{% endif %}
{% if is_mobile_view() %}
    <link rel="canonical" href="{{ full_view_url() }}" />
{% endif %}

Usage Example:

Setting up redirection to and from a mobile site that is the same Symfony 2 instance as your main site.

In this example, let's assume that you have a website and you wish to activate redirection to a mobile site when the user is using a mobile device.

Additionally, when a user with a desktop browser reaches the mobile site, he should be redirected to the full version at

  1. Set up mobile redirection to your config.yml

                is_enabled: true
                status_code: 301
                action: redirect
            tablet: ~
        switch_device_view: ~

    Now when you hit with a mobile device, you are redirected to At this point if the is configured to point to your project, you will get circular reference error. To get rid of the circular reference error, we want to disable mobile redirecting when we land on our mobile site.

  2. Create a new app.php file with a name like, for example, app_mobile.php and change the following:

    $kernel = new AppKernel('prod', false);


    $kernel = new AppKernel('mobile', false);

    Now your mobile site has its own environment and we can nicely create some custom configuration for it, disable mobile redirecting and activate desktop redirection instead.

  3. Create config_mobile.yml next to your config.yml and disable mobile redirecting. This should take care of the circular reference errors. Adding the full configuration activates desktop redirection.

    Also you might want to define your routing file as mobile specific. If you do, just create new routing_mobile.yml file and use it just like the default routing.yml. This gives you nice opportunity to route requests to custom mobile specific controllers that can render views that are designed for mobile. This way you don't need to write platform specific conditions to your view files.

            resource: "%kernel.root_dir%/config/routing_mobile.yml"
                is_enabled: false
            tablet: ~
                is_enabled: true
        switch_device_view: ~
  4. Configure your http server: Make sure that in your http server virtual host, you make use app_mobile.php as its script file instead of app.php.

    After you have restarted your http server everything should work. Also remember to clear the cache if you do changes to configs or you might end to get frustrated for nothing.


  • Write more tests
  • Add twig function url_for_switch_view('mobile') for generating switch view url's