As far as I can see there's no way to publish assets through console (Yii command). I have a very valid use case for the functionality:
We are running Yii application in docker cluster (production) and we have following simplified scheme: Nginx container (which manages vhosts for Yii advanced app) -> PHP application container -> DB container.
The problem is I need to bundle static assets (build into) with nginx container, because if I share static assets through any type of volume in production cluster (even in our staging environment), we will have a situation where they're not updating with Yii application container updates, as they're overlayered with volume. Currently we have a hacky script that does our syncing, but I'm sure that this and there are other valid use cases to have console command in Yii core to publish assets manually (bundling is still an option, but I don't want to bundle my assets currently, but to do the same as AssetManager does to publish but manually).
Laravel for example has php artisan vendor:publish
command to do it. The difference (problem) with Yii is that if for example Laravel knows what to publish as you add your extensions to Providers (some type of config) before using, in Yii we use assets provided by extensions simply registering their bundles in views. So the way to do it in Yii would be to have a config in console which explicitly states the assets that we need to publish.
If you agree that this functionality is helpful to core, I'm ready to give a PR as I'm going to do it in any case, to remove our hacky script from our production, which creates problems for us from time to time.
You can use asset command to combine your assets from console, it will publish all necessary assests in the process.
@klimov-paul Yes, I know, but as I have already mentioned there is not currently way to simply publish assets without combining them (do the same thing as AssetManager does on runtime by default).
Asset bundling is a way to go for production usually, but not always. As if you have big number of assets it is better not to combine them and load them on every page. I know that I can also use groups, to separate them to groups, but I suggest to simply add a way to publish assets with the same config without combining them (or doing any compressing, or other things) them.
I don't need minification and compressing for production on Yii side, as we use Nginx Pagespeed optimization module which does all the magic there (even combining when we need it).
You can setup jsCompressor
and cssCompressor
to some dummy command like pwd
to remove any processing.
Still for the pure asset publishing you can call particular asset class publish()
method:
https://github.com/yiisoft/yii2/blob/master/framework/web/AssetBundle.php#L182
e.g.:
$asset = JQueryAsset();
$asset->publlish(Yii::$app->assetManager);
I do not consider your use case to be common enough to justify yet another built-in console command introduction.
You can easily create your own extension for this and use it at will.
I'm in the same situation, where I've a deploy script, I'm using asset command to combine assets but some simply can't be combined. While I appreciate and will use the publish()
suggestion would be nice to have a generic command.
@armpogart good to see someone with the same problems.
Our setup is
The Nginx is serving multiple projects (mainly the same project but with other configurations set, beta testing for example), we dont have any assets on nginx, because that would mean, we have to recreate nginx on any build on any project, this is leading downtime to projects that are not touched.
The assets are served via:
AssetManager BeforeCopy Event -> leads to problems, because of concurrent requests.
And here i am, searching a way to publish all assets without combining, minifing whatever via buildphase of php images
@ItsReddi Yep, I'm rebuilding nginx image only on dev and staging environments, though I'm sure if I have some time I will come up with better solution, like the one you mentioned (though I need to find solution using dockerized environments, research those docker volume drivers finally....).
Not sure I understand precisely how your environment is working. I would be grateful if you have some time to chat about it elsewhere. For assets publishing I've come up with a little bit hacky solution, but it works for now great.
A suggestion: sam-it/yii2-static-assets. It builds a container containing all assets and ngingx ;)
We had our Yii application in Vagrant (and Ansible install on production), but recently we moved to Docker and are facing this issue too. We would like to publish assets when building container so all assets are available right after container is deployed, without any need for dumping files on request.
Right now it requires some dirty hacks to be working, IMO it should be supported out-of-the-box.
Most helpful comment
We had our Yii application in Vagrant (and Ansible install on production), but recently we moved to Docker and are facing this issue too. We would like to publish assets when building container so all assets are available right after container is deployed, without any need for dumping files on request.
Right now it requires some dirty hacks to be working, IMO it should be supported out-of-the-box.