A
A
Arnold2015-12-10 16:26:04
Yii
Arnold, 2015-12-10 16:26:04

Yii2 Are there any tools to automatically connect resources?

I remember for the first version on Habré there was habrahabr.ru/post/150885
Who else met any options? I would like to familiarize myself and not write a bicycle.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arnold, 2015-12-11
@Yii2SlowMoooo

The task is very secondary, so I ask everyone to comb my result)).
Until I got out of the situation like this.
Added class assets/AutoAsset.php :

<?php

namespace backend\assets;
use Yii;

/**
 * Class AutoAsset
 * Класс автоматически подключает к представлению модуля файлы ресурсов JS и CSS
 * По умолчанию путь к автоматически подключаемым ресурсам
 * @web/auto_asset/modules/{moduleName}/{viewName}/*.[js | Css]
 * Для подключения в представлении вызываем: AutoAsset::registerFiles($this)
 *
 * @author
 */
class AutoAsset
{
    public static $baseUrl = 'auto_asset/modules';

    /**
     * Формируем путь к ресурсам и подключаем все *.js и *.css фаилы в объекте $view
     * У всех зависимость от jQuery
     *
     * @param $view
     */
    public static function register($view) 
    {
        $viewFile = explode(DIRECTORY_SEPARATOR, $view->getViewFile());
        $viewFile = end($viewFile);
        $filePath =
            self::$baseUrl . "/" .
            Yii::$app->controller->module->id . "/" .
            str_replace(".php", "", $viewFile) . "/";

        // TODO Причесать монстра $realDirPath;
        $realDirPath = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . self::$baseUrl . DIRECTORY_SEPARATOR . Yii::$app->controller->module->id . DIRECTORY_SEPARATOR . str_replace(".php", "", $viewFile) . DIRECTORY_SEPARATOR;

        $dir=opendir($realDirPath);
        while ($file = readdir($dir)){
            if (strstr($file, ".js")) {
                $view->registerJsFile($filePath . $file ,['depends' => [\yii\web\JqueryAsset::className()]]);
            } elseif (strstr($file, ".css")) {
                $view->registerCSSFile($filePath . $file ,['depends' => [\yii\web\JqueryAsset::className()]]);
            }
        }
        closedir($dir);

    }
}

PS I apologize for the comments in Cyrillic, the company's requirement ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question