【问题标题】:Yii2 Theme Integration not working?Yii2 主题集成不起作用?
【发布时间】:2015-09-27 16:03:55
【问题描述】:

Yii2 主题集成?

'view' => [
    'theme' => [
        'pathMap' => ['@app/views' => '@app/admin/views'],
        'baseUrl' => '@web/admin',
    ],
],

【问题讨论】:

  • 有什么问题,显示你的组件代码。你在做什么,你的目标是什么我们无法想象你想要什么......
  • @Deepak Syal : 如果有什么安慰的话,我发现 Yii2 主题系统完全过于复杂并且不是非常基于惯例。每次都很头疼……

标签: yii2


【解决方案1】:

希望您使用的是高级模板

在后端文件夹中添加一个文件夹主题

使用主题名称创建一个子文件夹,并确保该文件夹中有 layouts 文件夹

即 您的新 布局文件夹 路径将是

后端/主题/主题文件夹名称/布局

在文件夹backend/config/main.php

 'components' => [

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

如果你想将它保存在 web 文件夹中,你也可以这样做,但请确保相应地更改路径

【讨论】:

  • 如果我不使用高级模板怎么办??
【解决方案2】:

预先模板有用于前端和后端主题集成的单独配置。

前端主题集成 => "frontend/config/main.php" 文件:

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

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

后端主题集成 => "backend/config/main.php" 文件:

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

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

虽然编码处理 cmets 和目录路径,但无需编写 baseUrl 或 basePath。

【讨论】:

    【解决方案3】:

    在网页目录中创建“主题”目录并在那里创建主题。

    然后将此代码包含在您的主配置文件中。

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

    【讨论】:

      【解决方案4】:

      在您的 web.php 文件中使用此代码。

      'view' => [
                  'theme' => [
                      'class' => yii\base\Theme::className(),
                      'basePath' => '@app/themes/themename',
                      'baseUrl' =>'@web/themes/themename',
                  ],
              ],
      

      【讨论】:

        【解决方案5】:

        这是我通常用于主题化的代码。您可以在 params 文件中设置参数,并在此处或直接在下面的代码中添加主题名称。

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

        【讨论】:

          【解决方案6】:

          如果你使用的是 yii2 basic,那么在 config/web.php 中写下这个

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

          【讨论】:

            【解决方案7】:

            我有 adminlte 主题,可以找到供应商文件夹, 然后在 config/main.php 中添加了这个:

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

            【讨论】:

              【解决方案8】:

              在 config/web.php 下的基本安装中,在组件块下添加代码。

              'components' => [
                             ................
                             ....................
                  'view' => [
                      'theme' => [
                          'pathMap' => [
                              '@app/views' => '@app/themes/mytheme',
                              '@app/modules' => '@app/themes/mytheme/modules',
                          ],
                          'baseUrl' => '@web/themes/mytheme',
                      ],
                  ],
               ...........
              ]
              

              【讨论】:

                【解决方案9】:

                有关安装主题和设置,请参阅下面的链接。

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

                【讨论】:

                  【解决方案10】:

                  我使用高级模板为我的前端设置了一个主题。我的主题位于我为存储主题而创建的主题文件夹中。例如。 web/themes/cerulean。正如我见过的一些键/值对所暗示的那样,任何单个主题文件夹下都没有物理视图文件夹,例如。 ['@app/views' => '@webroot/themes/themename/views]。事实上,我的代码在有和没有 value 的 views 子文件夹的情况下运行。这是我的工作code => @webroot/themes/cerulean,而不是@webroot/themes/cerulean/views,但它确实需要密钥的views 子文件夹。 IE。 @app/views。我已经测试了这两种变体,它们都有效,所以不要担心你是否对值的结尾有看法。

                  因为我在前端使用了一个主题,所以我将上面的@app/views 替换为@frontend/views。这是我在frontend/config/main.php 文件中的代码。

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

                  这是我的frontend\assets\appasset.php 文件中的代码:

                  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',
                      ];
                  }
                  

                  你会注意到上面我已经替换了

                  public $baseUrl = '@web'; 
                  

                  我在顶部设置了一个别名@themes,即...

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

                  上面代码中的baseurl现在设置为@themes,它实际上代表@web/themes/cerulean',取自frontend/configmain.php文件中的'view' => 'theme'设置。

                  【讨论】:

                    猜你喜欢
                    • 2015-12-04
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-01-21
                    • 1970-01-01
                    相关资源
                    最近更新 更多