6

Yii2 Theme Integration ?

'view' => [
    'theme' => [
        'pathMap' => ['@app/views' => '@app/admin/views'],
        'baseUrl' => '@web/admin',
    ],
],
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
Deepak Syal
  • 127
  • 1
  • 7

10 Answers10

9

Hope you are using the advanced template

add a folder themes in the backend folder

make a subfolder with the theme name and make sure you have the layouts folder in that folder

ie your new layout folder path will be

backend/themes/themefoldername/layouts

in the folder backend/config/main.php

 'components' => [

        'view' => [
            'theme' => [
                'basePath' => '@backend/themes/themefoldername',
                'baseUrl' => '@backend/themes/themefoldername',
                'pathMap' => [
                    '@backend/views' => '@backend/themes/themefoldername',
                ],
            ],
        ],...

if you want to keep it in the web folder also you can do that,but make sure you change the path accordingly

Midhun
  • 3,850
  • 1
  • 23
  • 26
1

In advance template there is separate configuration for frontend and backend theme integration.

Frontend theme integration => "frontend/config/main.php" file :

'components' => [ 
    'view' => [
            'theme' => [

                'pathMap' => [
                    '@frontend/views' => '@themes/frontend/views', // need to // set alias first in your bootstrap.php file
                ],
            ],
        ],
],

Backend theme integration => "backend/config/main.php" file :

'components' => [ 
    'view' => [
            'theme' => [

                'pathMap' => [
                    '@backend/views' => '@themes/backend/views', // need to set // alias first in your "common/config/bootstrap.php" file
                ],
            ],
        ],
],

While coding take care of comments and directory paths and no need to write baseUrl or basePath.

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
0

create "themes" directory in web directory and create theme there.

then include this code in your main config file.

'view' => [
            'theme' => [
                'baseUrl' => '@web/themes/yourthemename',
                'pathMap' => [
                    '@app/views' => [
                        '@webroot/themes/yourthemename/views',
                    ]
                ],
            ],
]
Abhishek
  • 689
  • 3
  • 14
0

use this code in your web.php file.

'view' => [
            'theme' => [
                'class' => yii\base\Theme::className(),
                'basePath' => '@app/themes/themename',
                'baseUrl' =>'@web/themes/themename',
            ],
        ],
sarin surendran
  • 303
  • 2
  • 12
0

Here is my code which i normally use for themeing. You can set param in params file and add theme name there or directly in the below code.

'view' => [
        'theme' => [
            'pathMap' => ['@app/views' => '@webroot/themes/themename/views'],
            'baseUrl' => '@web/themes/themename',
        ],
    ],
Awais Mustafa
  • 151
  • 2
  • 9
0

if you are using yii2 basic then in config/web.php write this

return [
    'components' => [
        'view' => [
            'theme' => [
                'basePath' => '@app/themes/basic',
                'baseUrl' => '@web/themes/basic',
                'pathMap' => [
                    '@app/views' => '@app/themes/basic',
                ],
            ],
        ],
    ],
];
jinal
  • 1
  • 1
0

I have the adminlte theme this be find the vendor folder, then in the config/main.php added this:

'components' => [         
     'view' => [
     'theme' => [
         'pathMap' => [
            '@app/views' => '@vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app'
         ],
     ],
    ],
HaroldV
  • 1
  • 1
0

In basic installation under config/web.php add the code under component block.

'components' => [
               ................
               ....................
    'view' => [
        'theme' => [
            'pathMap' => [
                '@app/views' => '@app/themes/mytheme',
                '@app/modules' => '@app/themes/mytheme/modules',
            ],
            'baseUrl' => '@web/themes/mytheme',
        ],
    ],
 ...........
]
Vikram
  • 35
  • 1
  • 10
0

Refer below link for install theme and setup.

http://banoprogrammer.blogspot.in/2017/07/backend-theme-installation.html

Naushil Jain
  • 434
  • 3
  • 11
0

I set up a theme for my frontend using the advanced template. My themes are located in the themes folder which I created for storing the themes. eg. web/themes/cerulean. There are NO physical view folders under any of the individual theme folders as might be suggested by some of the key/value pairs that I have seen eg. ['@app/views' => '@webroot/themes/themename/views]. In fact, my code runs with and without value's views subfolder. This is my working code => @webroot/themes/cerulean as opposed to @webroot/themes/cerulean/views but it does need key's views subfolder. ie. @app/views. I have tested both of these variations and they both work so do not be concerned whether you have a view on the end of the value or not.

Because I am using a theme for the frontend I have replaced @app/views as above with @frontend/views. This is my code in my frontend/config/main.php file.

 'view' => [
            'theme' => [
                'pathMap' => ['@frontend/views' => '@webroot/themes/cerulean',],
                'baseUrl' => '@web/themes/cerulean',                
            ],
        ], 

This is the code in my frontend\assets\appasset.php file:

namespace frontend\assets;

use yii\web\AssetBundle;

use Yii;
Yii::setAlias('@themes', Yii::$app->view->theme->baseUrl);

/**
* Main frontend application asset bundle.
*/
class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    //public $baseUrl = '@web';
    public $baseUrl = '@themes';
    public $css = [
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

You will notice above that I have substituted the

public $baseUrl = '@web'; 

with an alias @themes which I have set at the top namely...

Yii::setAlias('@themes', Yii::$app->view->theme->baseUrl);

The baseurl in the code above is now set to @themes which actually represents @web/themes/cerulean' taken from the 'view' => 'theme' setting located in the main.php file under frontend/config.

Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
Addi
  • 199
  • 14