L
L
Lev K2017-06-20 15:02:03
Yii
Lev K, 2017-06-20 15:02:03

How to create multiple themes in yii2?

Hello.
Faced the problem of creating multiple themes, now in the config:

'components'=>[
'view' => [
            'theme' => [
                'basePath' => '@app/themes/theme1',
                'baseUrl' => '@app/themes/theme1/web',
                'pathMap' => [
                    '@app/views' => '@app/themes/theme1/views',
                ],
            ],
        ],
],

How can I add theme2 ? basePath & baseUrl must be different because different themes have different css and js files that are stored in */web/css|js|image.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-06-20
@Leffken

Good afternoon.
Once, too, was interested in this topic. There are several options for solving the problem.
For example, in SiteController you can do this:

public function init() 
    {
        parent::init();
        // Вариант 1
        // Работает без компонента в конфигурации
        // 'view' => [
        //    'theme' => []
        // ]
        $theme = Yii::$app->request->get('theme');
        if($theme){
            $this->getView()->theme = Yii::createObject([
                'class' => '\yii\base\Theme',
                'baseUrl' => '@web/themes/'.$theme,
                'basePath' => '@app/themes/'.$theme,
                'pathMap' => ['@app/views' => '@app/themes/'.$theme],
             ]);
         }
        
        // Вариант 2
        //Работает, если в конфигурации есть компонент без параметров
        // 'view' => [
        //    'theme' => []
        // ]
        Yii::$app->view->theme->basePath = '@app/themes/admin';
        Yii::$app->view->theme->baseUrl = '@web/themes/admin';
        Yii::$app->view->theme->pathMap = ['@app/views' => '@app/themes/admin'];

    } 

    // Вариант 3
    // устанавливает тему на лету
    // вид ссылки
    // <a href="themeswitch/theme=theme_name">theme_name</a>
    // 
    public function actionThemeswitch($theme)
    {

        if($theme == 'default'){
            Yii::$app->response->cookies->remove('theme');
            return $this->redirect(['index']);
        }

        $options = ['name'=>'theme','value'=>$theme,'expire'=>time()+86400*365];
        $cookie = new \yii\web\Cookie($options);
        Yii::$app->response->cookies->add($cookie);

        return $this->redirect(['index']);
    }

    public function beforeAction($action)
    {
        if (parent::beforeAction($action)) {
            if (Yii::$app->request->cookies['theme']) {
                $theme = Yii::$app->request->cookies->getValue('theme');

                Yii::$app->view->theme = new \yii\base\Theme([
                    'pathMap' => ['@app/views' => '@app/themes/'.$theme],
                    'baseUrl' => '@web',

                ]);

            }
            return true;  // or false if needed
        } else {
            return false;
        }
    }

Another option How to make your own themes for a yii2 project?
My example (option No. 3)
PS The example for option No. 3 was not invented by me personally. I found it on the Internet, maybe even on toster.ru. Showed as demo.
PSS
Let's say Portfolio theme (link.)
Theme is located at themes/portfolio
Images for themes/portfolio/assets/ims theme (css, js are also located there)
Theme views in themes/portfolio/views/site
In the theme's index.php file
use app\assets\PortfolioAsset;
 $image = PortfolioAsset::register($this);
 $theme = $this->theme;

I connect the images in the same file like this:
echo Html::img($image->baseUrl.'/img/portfolio-1.jpg',
                         ['alt' => '']);

And of course, in PortfolioAsset there is such a parameter
public $sourcePath = '@app/themes/portfolio/assets';

M
Maxim Timofeev, 2017-06-20
@webinar

As an option:

'view' => [
     'theme' => require(__DIR__ . '/theme.php'),
]

there lies the config and is overwritten from the admin panel

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question