diff --git a/DeviceDetect.php b/DeviceDetect.php index 527365c..a2cdebd 100644 --- a/DeviceDetect.php +++ b/DeviceDetect.php @@ -16,6 +16,9 @@ class DeviceDetect extends \yii\base\Component { private $_mobileDetect; + // Automatically set view parameters based on device type + public $setParams = true; + public function __call($name, $parameters) { return call_user_func_array( array($this->_mobileDetect, $name), @@ -30,17 +33,18 @@ public function __construct($config = array()) { public function init() { $this->_mobileDetect = new MobileDetect(); parent::init(); - - \Yii::$app->on(\yii\base\Application::EVENT_BEFORE_REQUEST, function($event){ - \Yii::$app->params['devicedetect'] = [ - 'isMobile' => \Yii::$app->devicedetect->isMobile(), - 'isTablet' => \Yii::$app->devicedetect->isTablet() - ]; - - \Yii::$app->params['devicedetect']['isDesktop'] = - !\Yii::$app->params['devicedetect']['isMobile'] && - !\Yii::$app->params['devicedetect']['isTablet']; - }); + + if ($this->setParams) + \Yii::$app->on(\yii\base\Application::EVENT_BEFORE_REQUEST, function($event){ + \Yii::$app->params['devicedetect'] = [ + 'isMobile' => \Yii::$app->devicedetect->isMobile(), + 'isTablet' => \Yii::$app->devicedetect->isTablet() + ]; + + \Yii::$app->params['devicedetect']['isDesktop'] = + !\Yii::$app->params['devicedetect']['isMobile'] && + !\Yii::$app->params['devicedetect']['isTablet']; + }); } }