Yii2 How to Configure a Theme

A theme is directory of view and layout files. Sometimes one application may have more than one theme defending upon the requirements of the application.

Lets say, you want to create a theme named ‘test‘ and configure it with your application then:

  • In your application root, create a directory named ‘themes
  • Inside it create another directory named ‘test‘ (this will be your theme directory)
  • Copy ‘layouts‘ directory from ‘views‘ directory in your application and paste it in your newly created theme directory ‘themes/test
  • Open your configuration file and paste the following lines:

In the above, pathMap defines a map of original paths to themed paths while baseUrl defines the base URL for resources referenced by theme files.

In our case pathMap is [‘@app/views’ => ‘@app/themes/test’]. That means that every view in @app/views will be first searched under @app/themes/test¬†and if a view exists in the theme directory it will be used instead of the original view.

Happy Coding!

3 thoughts on “Yii2 How to Configure a Theme

    1. admin Post author

      In the Yii2, for the security reason they did not want to make the web folder accessible from the web. So, one way is use create a folder ‘test’ (which is the name I have used in the above tutorial). Then goto assets/AppAsset.php and make the changes like the following:

      class AppAsset extends AssetBundle
      {
      public $basePath = '@webroot';
      public $baseUrl = '@web/test';
      public $css = [
      'css/site.css',
      ];
      public $js = [
      ];
      public $depends = [
      'yii\web\YiiAsset',
      'yii\bootstrap\BootstrapAsset',
      ];
      }

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.