Skip to content

deanstalker/MobileDetectBundle

 
 

Repository files navigation

MobileDetectBundle

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

Introduction

This Bundle use php-mobile-detect (fork of Mobile_Detect class) and provides the following features:

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

Installation

Composer

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

...
    {
        "suncat/php-mobile-detect": "2.0.9",
        "suncat/mobile-detect-bundle": "dev-master"
    }
...

Run command: php composer.phar install

Submodules

Add this bundle and php-mobile-detect library to your project as Git submodules:

$ git submodule add git://github.com/suncat2000/php-mobile-detect.git vendor/php-mobile-detect
$ git submodule add git://github.com/suncat2000/MobileDetectBundle.git vendor/bundles/SunCat/MobileDetectBundle

Register the namespace SunCat to your project's autoloader bootstrap script:

//app/autoload.php
$loader->registerNamespaces(array(
    // ...
    'SunCat' => __DIR__.'/../vendor/bundles',
    // ...
));

$loader->registerPrefixes(array(
    // ...
    'Mobile_' => __DIR__.'/../vendor/php-mobile-detect/lib',
    // ...
));

Add this bundle to your application's kernel

//app/AppKernel.php
public function registerBundles()
{
    return array(
         // ...
        new SunCat\MobileDetectBundle\MobileDetectBundle(),
        // ...
    );
}

Configure service in your YAML configuration

#app/config/config.yml
mobile_detect:
    redirect:
        mobile: ~
        tablet: ~

Full configuration

#app/config/config.yml
mobile_detect:
    redirect:
        mobile:
            is_enabled: true # default false
            host: http://m.site.com # with scheme (http|https), default null, url validate
            status_code: 301 # default 302
        tablet:
            is_enabled: true # default false
            host: http://t.site.com # with scheme (http|https), default null, url validate
            status_code: 301 # default 302

PHP examples

Check type device

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

Check phone

is[iPhone|HTC|Nexus|Dell|Motorola|Samsung|Sony|Asus|Palm|Vertu|GenericPhone]

$mobileDetector->isIphone();
$mobileDetector->isHTC();
etc.

Check tablet

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

$mobileDetector->isIpad();
$mobileDetector->isMotorolaTablet();
etc.

Check mobile OS

is[AndroidOS|BlackBerryOS|PalmOS|SymbianOS|WindowsMobileOS|iOS|badaOS]

$mobileDetector->isAndroidOS();
$mobileDetector->isIOS();

Check mobile browser User-Agent

is[Chrome|Dolfin|Opera|Skyfire|IE|Firefox|Bolt|TeaShark|Blazer|Safari|Midori|GenericBrowser]

$mobileDetector->isChrome();
$mobileDetector->isSafari();

Twig Helper

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

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 %}

TODO

  • Add config param detect_tablet_how_mobile (default false) and fix detect functional for support this param
  • Add twig function url_for_switch_view('mobile') for generation switch view url's

About

Symfony2 bundle for detect mobile devices with the ability to redirect to the mobile version.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published