xenogenesi::blog
memento
3d adt android apache2 app apt aria2 build bullet cflags chromium codeigniter debian demoscene dependencies dpkg driver emulator freeglut gcc gfx git glut htaccess javascript json kernel linux make metalink minimal mysql opengl php python raspbian realtime rpi specs template toolchain update-alternatives video wifi wordpress

Yii2: more about assets

Lun, 25 Dic 2017 17:22:12 +0100

Yii2’s guide about assets is good and explain how to compress, convert and combine bundles but lack some information, not every asset can be combined (for instance, javascripts with some custom dynamic module/plugins loading may cause some trouble).

The defaul hash() function:

$path = (is_file($path) ? dirname($path) : $path) . filemtime($path);
return sprintf('%x', crc32($path . Yii::getVersion() . '|' . $this->linkAssets));

it evaluates the directory full path and the file/directory mtime, Yii’s version and the option linkAssets, this mean if your development and production server have different paths the asset will need to be published again (as mentioned in the guide the option hashCallback could be used to modify this behavior).

You may want (as I do) to process your development environment to generate a production directory and publish all assets, even the one not combined, in the current Yii2 implementation there’s nothing ready to do so, you need to add a console command, something like that:

namespace app\commands;

use Yii;
use yii\console\Controller;

class PublishController extends Controller
{
    public function actionIndex($assetName = null) {

        Yii::setAlias('@webroot', __DIR__ . '/../web' );
        Yii::setAlias('@web', '/');

        if ($assetName != null) {
            echo "* publishing:  '$assetName'\n";
            $asset = new $assetName();
            $asset->publish(Yii::$app->assetManager);
            echo "  - sourcePath: $asset->sourcePath\n";
            echo "  - baseUrl: $asset->basePath\n";
        }
    }

then call it from the shell or a shell script (using the same namespace and class name used by Yii2’s loader to find the asset):

php yii publish noam148\\imagemanager\\assets\\ImageManagerAsset