Yii Framework Forum: Theme Switching - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Theme Switching How to dynamically switch themes

#1 User is offline   Jim K 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 18-December 13

Posted 19 March 2017 - 06:06 PM

I'm having trouble converting functionality I have in my Yii1.1 app into a Yii2. Specifically it's around switching themes dynamically using dkeeper/yii2-mobiledetect. In version Yii1.1, I was able to add my mobile/tablet/classic views under a @app\themes\{theme name folder} like this...
myapp\
  protected\
    views\
      wines\
        index.php
  themes
    mobile\
      css\
      views\
        wines\
          index.php
    tablet\
      css\
      views\
        wines\
          index.php
    classic\
      views\
        wines\
          index.php

In Yii1.1 @app\components\Controller.php I updated the init() method to:

    public function init()
    {
        //read and store current user data so it's available in all controllers
        if (!Yii::app()->user->isGuest)
        {
            $this->userData = User::model()->findByPk(Yii::app()->user->id);
        }
        
        //theme defaults to classic in config/main.php
        //we set it here to either mobile or tablet as appropriate

        $detector = Yii::app()->mobileDetect;

        if ($detector->isTablet())
        {
            Yii::app()->theme = 'tablet';
        }
        elseif ($detector->isMobile())
        {
            Yii::app()->theme = 'mobile';
        };
        parent::init();
    }


This worked just fine. The views automatically switched based on the type of browser detected by MobileDetect.

I need some guidance on how to accomplish the same in Yii2. I'm using Kartik's version of the basic template kartik-v/yii2-app-practical-b. I've been able to add a new themes folder under "Source Files" and updated the theme info in config\web.php components[]view[] and it works just fine for 1 theme, I'm just not sure where/how to change it based on mobiledetect.

The MobileDetect install has me update the @app\index.php like this. My first thought is to use YII::$app->set() to update the components just before application->run(). Is there a better way to do this?

$config = require(__DIR__ . '/config/web.php');

$application = new yii\web\Application($config);

Yii::$app->on(\yii\base\Application::EVENT_BEFORE_REQUEST,function($event){
    Yii::$app->params['detect'] = [
        'isMobile' => Yii::$app->mobiledetect->isMobile(),
        'isTablet' => Yii::$app->mobiledetect->isTablet(),
    ];
});

$application->run();


Thanks,
Jim
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,947
  • Joined: 17-January 09
  • Location:Russia

Posted 19 March 2017 - 06:13 PM

https://github.com/s...ching-themes.md
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users