I have changed configuration in Symfony2 in order to dump assets in AWS S3.
# app/AppKernel.php
/**
* {@inheritdoc}
*/
public function boot()
{
parent::boot();
$s3client = new \MyVendor\SiteBundle\Entity\S3AssetManager(
$this->container->getParameter('aws_access_key'),
$this->container->getParameter('aws_secret_key'),
$this->container->getParameter('aws_s3_region')
);
$s3client->registerStreamWrapper();
}
And:
# app/config_prod.yml
framework:
templating:
assets_base_url: "http://myAssetsHost.com/"
assetic:
write_to: 's3://myAssetsHost.com'
Here's what I use in my views:
{% block stylesheets %} <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans&subset=latin,greek" /> {% stylesheets 'bundles/mysite/css/screen.css' filter='cssrewrite' %} <link rel="stylesheet" href="{{ asset_url }}" /> {% endstylesheets %} {% endblock %}
It works fine in frontend, and my urls are like this:
In backend (/admin/dashboard), urls are generated like this:
http://myAssetsHost.com/bundles/sonataadmin/vendor/bootstrap/dist/css/bootstrap.min.css
which result in 404 because files are not generated. Sonata (default) standard_layout.html.twig:
{% for stylesheet in admin_pool.getOption('stylesheets', []) %}
<link rel="stylesheet" type="text/css" href="{{ asset(stylesheet) }}" />
{% endfor %}
During deployment I use these two commands to dump assets:
php app/console cache:clear --env=prod php app/console assetic:dump --env=prod
How can I make Assetic pick up all bundle's assets in order to publish them to S3?
Here's how I solved it:
since twig's asset() does not generate same URL as assetic
stylesheets
&javascripts
block, I created a file that overridesstylesheets
&javascripts
block:Note, it does not call
{{ parent() }}
since I don't want to load non existing files.The only problem with this approach is that it completely ignores
admin_pool.getOption('stylesheets', [])
admin_pool.getOption('javascripts', [])
so if a new stylesheet/js file is added in next SonataAdminBundle version, I will have to keep an eye and update my file as well.
Unfortunately,
{% stylesheets %}
and{% javascripts %}
are both unable to handle arrays. If that was possible, you could do: