I started to build a website with Symfony2, and I had a little bit of a quandary about resources.
The Symfony2 Book says that every resource file have to be in a Bundle, but what about the most essential images, .js and .css files that I use in the base.html.twig file on every page?
Should I make a CoreBundle or something similiar, just for these files, or should I put these in the app\Resources folder (or directly in the web folder, maybe)? If I can use the app\Resources folder for these, how can I reference these files from the template?
Making a Bundle just for this seems a little unnecesary, and the asset urls for these files are ugly too (e.g. '/bundles/projectcore/images/logo.jpg') in my opinion.
What's the best practice here?
I've had situations where I've kept all files in /web (i.e. /web/js) and others where I've kept them within an 'Assets' bundle.
If you're developing a bundle that's going to be reused in numerous projects, it makes sense to store the assets in that bundle. I think you would then publish/install those assets to the web folder using the command line. For example, let's say you had a BlogBundle that required specific css. You would store the css in that bundle, so the next time you use BlogBundle for a project you could easily reuse the css.
As with many other things with Symfony2, your personal preference plays a big part in these decisions. I recommend staying consistent though with where you store your assets. Having to manage assets split in three different locations (web, AssetsBundle, other bundles) could be a big headache. So pick a location, and try to stay consistent.
As for accessing assets from app/Resources... you may be able to use Assetic for this. I'm not very familiar with it, but I believe you can load assets from anywhere within your project. I'd actually recommend taking a look at the core Assetic code (look in vendor\Assetic
) instead of the Symfony2 Assetic helper because you'll get a better idea of just what's possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With