Mobile versions

This is a continuation of this thread http://www.yiiframework.com/forum/index.php?/topic/11187-mobile-browser-detection/page__pid__117415#entry117415. Since I have taken it from its original topic, I thought I would continue it here…

I’ve used Mike’s suggestion from the above thread of adding the following code to my components/Controller.php to detect mobile devices and display a mobile version, and it works great.


private $_isMobile;


const RE_MOBILE='/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220)/i';


public function getIsMobile()

{

    if ($this->_isMobile===null)

        $this->_isMobile=isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']) ||

            preg_match(self::RE_MOBILE, $_SERVER['HTTP_USER_AGENT']);

    return $this->_isMobile;

}


public function init()

{

    if ($this->getIsMobile())

        $this->layout='//layouts/mobile'

}



However, I was wondering if there is a way to give mobile users the ability to switch back and forth between the mobile version and the full desktop version. I’ve tried several different things but nothing seems to work.

mdomba suggested…

That’s what I’ve been trying to do, but I can’t get it to work correctly. Instead of changing $this->layout like Mike did, I created a mobile theme and changed themes with


Yii::app()->theme = 'mobile';

That worked for the initial theme change to the mobile version, but I haven’t been able to get it to switch back based on a user’s input. I’m thinking that the issue is passing parameters to the init() function. Is it even possible to pass parameters to the init() function, or am I going about this the wrong way?

Thanks!

If you want to implement something like my idea…

Your init would look like


public function init()

{

    if(isset($_GET['dev']) && $_GET['dev']==='m' || $this->getIsMobile())

        $this->layout='//layouts/mobile'

}

Oh that makes perfect sense! I was hung up on passing the parameters and never thought of using the $_GET variable. Thanks!