Chewy Chewy - 6 months ago 117
PHP Question

Yii2 Asset bundle include html files

This is a very niche case, but I am trying to use Google's new Polymer web components framework in a test with Yii2. The components are installed with bower (and if possible, I'd like to maintain them this way as well), and are thus outside of the web-accessible zone under @vendor.

I originally thought I could create an AssetBundle to pull the resources out of the vendor/bower/, but it looks like the AssetBundle class only supports css and js files. Can this be overridden by making changes to the AssetBundle class? Is this even a good idea?

Or should I move these files to somewhere within the web-accessible space to save myself the headaches of trying to manage them?

Any help would be appreciated.


Yii2 assetManager will publish all the files in specified source directory, not only css and js. You could then simply build the needed link tags in your layout, e.g. :

Register your asset bundle :

$polymerAssetBundle = \app\assets\PolymerAssetBundle::register($this);

Or if you want to load it in head :

$this->registerAssetBundle(\app\assets\PolymerAssetBundle::className(), \yii\web\View::POS_HEAD);

Then you could import your polymer component like this :

<link rel="import" href="<?= $polymerAssetBundle->baseUrl ?>/path/to/your/component.html">

About baseUrl :

if you specify the sourcePath property, the asset manager will publish the assets and overwrite this property accordingly

Read more :