2

im using symfony ad i read that the best practice to store image js and images is /web

<link rel="stylesheet" href="{{ asset('css/bootstrap.min.css') }}" />

if I want use assetic i found example like

    {% javascripts '@AppBundle/Resources/public/js/*' output='js/compiled/main.js' %}
    <script src="{{ asset_url }}"></script>
{% endjavascripts %}

where @AppBundle is logical name of Bundle

Logical File Names¶

When you want to reference a file from a bundle, use this notation: @BUNDLE_NAME/path/to/file; Symfony will resolve @BUNDLE_NAME to the real path to the bundle. For instance, the logical path @AppBundle/Controller/DefaultController.php would be converted to src/AppBundle/Controller/DefaultController.php, because Symfony knows the location of the AppBundle.

Where Do I Have To store my assets?

3 Answers 3

3

If you are NOT using assetic, you should place your publicly accessible images and js in the /web directory.

If you are USING assetic, you should place your publicly accessible images and js in your @AppBundle/Resources/public/css and @AppBundle/Resources/public/js respectively. Then you will have to use assetic, assetic will automatically take care of making it accessible to the public. You don't need to worry about that, usually it will filter and copy your files into your /web directory.

Example:

{% block javascripts %}
    {% javascripts '@AppBundle/Resources/public/js/*' %}
        <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}

In this example, all files in the Resources/public/js/ directory of the AppBundle will be loaded and served from a different location. The actual rendered tag might simply look like:

<script src="/app_dev.php/js/abcd123.js"></script>

For more info, read this: http://symfony.com/doc/2.8/cookbook/assetic/asset_management.html#installing-and-enabling-assetic

Sign up to request clarification or add additional context in comments.

3 Comments

But with Assetic, you can manipulate these assets however you want (or load them from anywhere) before serving them. ok but what is the best practice? store in web o in bundle? because in this link symfony.com/doc/current/best_practices/… the assets are in web folder
I think it is safe to say that the use of Assetic is being discouraged as evidenced by the fact that it is no longer even bundled by default. If your assets are simple then just put then under /web/css and js. If they require some server side processing (minification etc) then use one of the many specialized nodejs tools such as gulp, webpack or browserify.
I agree with @Cerad. If you are going to reuse your bundles in another projects, then store it in your bundle.
1

you should put your css/js files under app/Resources/assets/css and app/Resources/assets/js (asset folder can be "public" folder)

And later you shoud use assetic to put the final app.css and app.js under the web folder (this is the public folder in symfony projects)

Documentation here (contradiction?) and here

4 Comments

Your answer contradicts your first link. Perhaps you could explain a bit more?
are you sure to put asset in app/Resources/assets/?
@cerad why? I put my assets (css and js) in my pirvate zone (app/Resources/assets) and later, with assetic I put the final files app.css and app.js in public web folder. I think, thats it's correct, ins't it?
Assetic is no longer delivered by default with Symfony. The best practices that you linked to discourages the use of assetic. Perhaps you should remove your first link and then make clear that your suggestions are contrary to what Symfony currently suggests. At the very least, removing the link would remove the rather serious contradiction in your answer.
1

Since Symfony4.0, the Best Practice is now in a new /assets directory in the root directory.

This makes it much easier for your Frontend developers and designers to work without having to get into the backend mess.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.