如何在 Yii2 中获取 JuiAsset 的主题文件夹路径


How to get the theme folder path of JuiAsset in Yii2?

在 yii2-advanced-app 中,jui 资产位于以下路径中: 找到themesui文件夹的web'assets'135efca3' -事实上,我不知道为什么 Yii2 会制作不同的路径段或假路径。我认为assetManager的getAssetPath()方法应该返回该路径,但我不知道如何返回!

我在控制器的actions()方法中尝试了以下调试代码:

public function actions()
{
   echo Yii::$app->assetManager->getAssetPath(Yii::$app->assetManager->bundles = [
          'yii'jui'JuiAsset'], 'themes');
          die(); //for debugging
} 

但是,它只是打印/themes.

换句话说,我可以问,我怎么能提供getAssetPath()的第一个参数(对象)?因为,我认为这是这里的问题。

编辑

我创建了以下助手 - 根据 arogachev 关于路径的答案 - 以获取主题列表。

<?php
namespace common'libs;
use yii;
use yii'web'Controller;
    class JuithemeHelpers
    {
      public static function getThemesList()
      {
        $themesPath =  dirname(Yii::$app->basePath).DIRECTORY_SEPARATOR."vendor".DIRECTORY_SEPARATOR."bower".DIRECTORY_SEPARATOR."jquery-ui".DIRECTORY_SEPARATOR."themes";
        $output = [];
        foreach (scandir($themesPath) as $item){
          if (is_dir($themesPath.DIRECTORY_SEPARATOR.$item) && ($item != '.' && $item !='..')) $output[] = $item;
        }
        return $output;
      }
    }

然后,在视图中,我做了以下几点:

...
<?= $form->field($model, 'birthdate')->widget(DatePicker::className(), ['clientOptions' => ['dateFormat' => 'yy-mm-dd', 'changeYear' => true, 'yearRange' => sprintf('%s:%s', date('Y')-100,date('Y')-16)],]) ?>
    
      <select onchange="changeTheme(this.value)">
      <?php foreach (JuithemeHelpers::getThemesList() as $item): ?>
              <option value="<?= $item ?>"><?= $item ?></option>
        <?php       endforeach; ?>
      </select>
    
    
    <?= $form->field($model, 'gender_id')->dropDownList($model->getGenderList(), ['prompt' => 'Please Select one...']) ?>

  ...
......
<script>
    function changeTheme(n){  
      s = document.getElementsByTagName('link');
      o = ''
      re = /'/themes'/(.*)'/jquery-ui.css/gi;
      for (i = 0; i < s.length; i++){
        if (s[i].href.match(re)){
          
                o = s[i].href.replace(re.exec(s[i].href)[1],n);
                s[i].href=o;
        }       
      }
    }  
      </script>

我认为现在是时候学习如何将所有这些打包到一个小部件中了。

jQuery UI(默认情况下随框架一起提供)主题文件夹位于/vendor/bower/jquery-ui/themes文件夹中。

您可以通过检查 yii''jui''JuiAsset 的$sourcePath$css属性来查看它。

您可以使用别名@bower,而不是编写完整路径:

Yii::getAlias('@bower/jquery-ui/themes');

对于列表,您可以使用例如此方法:

$themesPath = Yii::getAlias('@bower/jquery-ui/themes');
$results = scandir($themesPath);
foreach ($results as $result) {
    if ($result === '.' || $result === '..' || !is_dir($themesPath . '/' . $result)) {
        continue;
    }
    echo $result . "<br/>";
}