2017 © Pedro Peláez
 

yii2-extension yii2-mobile-detector

Mobile detector for Yii2 Framework.

image

analyticsface/yii2-mobile-detector

Mobile detector for Yii2 Framework.

  • Tuesday, April 4, 2017
  • by vladimirovii
  • Repository
  • 1 Watchers
  • 0 Stars
  • 23 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Aface Mobile Detect

Installation

The preferred way to install this extension is through composer. This requires the composer-asset-plugin, which is also a dependency for yii2 – so if you have yii2 installed, you are most likely already set., (*1)

Either run, (*2)

composer require analyticsface/yii2-mobile-detector:*

or add, (*3)

"analyticsface/yii2-mobile-detector" : "*"

to the require section of your application's composer.json file., (*4)

Usage

add rule for hide 'index' action, (*5)

'<controller:\w+>/' => '<controller>/index'

1) Add to /frontend/config/bootstrap.php, (*6)

Yii::$container->set('aface\mobiledetector\mode\ClientMode', function () {
    return new \aface\mobiledetector\mode\ClientMode(Yii::$app->request);
});

2) Add to 'components' view, (*7)

'components' => [
        'view' => function (\aface\mobiledetector\mode\ViewMode $mode) {
            return Yii::createObject([
                'class' => 'yii\web\View',
                'theme' => $mode->isMobile()
                    ? [
                        'basePath' => '@frontend/themes/mobile',
                        'baseUrl' => '@web',
                        'pathMap' => [
                            '@frontend/views' => '@frontend/themes/mobile/views',
                        ],
                    ]
                    : [
                        'basePath' => '@frontend/themes/desktop',
                        'baseUrl' => '@web',
                        'pathMap' => [
                            '@frontend/views' => '@frontend/themes/desktop/views',
                        ],
                    ],
            ]);
        },
        ...
    ]

3) Add filter 'as viewMode', (*8)

 'components' => [
        ...
    ],
    'as viewMode' => [
        'class' => 'aface\mobiledetector\filter\ViewModeFilter',
        'expire' => 30 * 86400 // 30 days
    ],
    'params' => $params,

4) Add to layout switch link, (*9)

<?= Html::a('Mobile version', Url::current(['mode' => 'mobile']), ['data-method' => 'post']) ?>

5) If css/js files used from theme, (*10)

a) add to main Asset, (*11)

public $sourcePath = '@theme';

b) add alias, (*12)

'components' => [
        'view' => function (\aface\mobiledetector\mode\ViewMode $mode) {

            $theme = Yii::$app->request->getCookies()->getValue(\aface\mobiledetector\mode\ClientMode::COOKIE_NAME);

            if ($theme === null) {
                $theme = $mode->isMobile() ? 'mobile' : 'desktop';
            }

            Yii::setAlias('theme', '@frontend/themes/' . $theme);

            ...   
        },
        ...
    ]

The Versions

04/04 2017

dev-master

9999999-dev

Mobile detector for Yii2 Framework.

  Sources   Download

BSD-3-Clause

The Requires

 

yii2 yii 2 mobile detector