Skip to content

Commit

Permalink
Merge pull request #3 from ilyapoz/set-params
Browse files Browse the repository at this point in the history
Set params
  • Loading branch information
alexandernst committed Apr 22, 2015
2 parents 79be161 + 7675d29 commit efb0513
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions DeviceDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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'];
});
}

}

0 comments on commit efb0513

Please sign in to comment.