Skip to content

Instantly share code, notes, and snippets.

@mdmunir
Last active January 6, 2021 01:46
Show Gist options
  • Save mdmunir/581d51e20d35d2bad41fa2a0eb6d2c85 to your computer and use it in GitHub Desktop.
Save mdmunir/581d51e20d35d2bad41fa2a0eb6d2c85 to your computer and use it in GitHub Desktop.
Publish asset tanpa hash folder
<?php
namespace app\classes;
use Yii;
use yii\base\Behavior;
use yii\web\AssetManager;
/**
* Description of AssetPublishBehavior
*
* @author Misbahul D Munir <[email protected]>
* @since 1.0
*/
class AssetPublishBehavior extends Behavior
{
/**
* sesuaikan kebutuhan
*/
public $maps = [
'@node_modules' => 'nm',
'@vendor' => 'v',
'@bower' => 'b',
'@npm' => 'nm',
'@app' => '',
];
protected $mapDirs = [];
public function init()
{
foreach ($this->maps as $path => $to) {
$path = rtrim(Yii::getAlias($path), '/') . '/';
$this->mapDirs[$path] = trim($to, '/');
}
krsort($this->mapDirs);
}
/**
*
* @param AssetManager $owner
*/
public function attach($owner)
{
parent::attach($owner);
$owner->hashCallback = [$this, 'hash'];
}
public function hash($path)
{
foreach ($this->mapDirs as $key => $to) {
if (strpos($path, $key) === 0) {
$subpath = substr($path, strlen($key));
return $to ? "$to/$subpath" : $subpath;
}
}
return ltrim($path, '/');
}
}
return [
...
'components' => [
...
'assetManager' => [
'as publish' => 'app\classes\AssetPublishBehavior'
]
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment