dev-master
9999999-devMobile detector for Yii2 Framework.
BSD-3-Clause
The Requires
yii2 yii 2 mobile detector
Mobile detector for Yii2 Framework.
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)
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); ... }, ... ]
Mobile detector for Yii2 Framework.
BSD-3-Clause
yii2 yii 2 mobile detector