1

I have write a bundle that contains public assets: Images, CSS and Javascript. Everything is stored in my bundle Resources/public/ folder.

I launch an ascetic command that generates symlinks in global web project folder.

My problem is my bundle name is visible in Images, CSS and Javascript URLs. Is there a way to rewrite this URL. I want to access to my CSS with this URL: http://mywebsite.com/mycustomfoldername/mycss.css

Second question: Is it a good thing to write directly files in global web project folder ?

testpresta
  • 429
  • 5
  • 15

2 Answers2

2

If you are using Symfony's built in command assets:install command it will change:

<script src="{{ asset('js/script.js') }}" type="text/javascript"></script>

into:

<script src="/bundles/your-bundle/js/script.js" type="text/javascript"></script>

But if you will use Assetic assetic:dump command (this is command provided by AsseticBundle) it will change:

{% javascripts '@YourBundleBundle/Resources/public/js/script.js' %}
    <script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

into (in production mode)

<script type="text/javascript" src="/js/as5s31l.js"></script>

And I guess it solves your issue of bundle name in url.

As for putting js files directly in main web folder: it is not good idea as everything should be bundle specific, all code sits in bundle so your assets should also.

Check this answer for some additional explanation about difference between assets:install and assetic:dump commands

Community
  • 1
  • 1
Tomasz Madeyski
  • 10,742
  • 3
  • 50
  • 62
  • In your case (ascetic:dump), the URL is not correct because the js filename is changed. I think this is due to avoid filename collision. I really want to get: – testpresta Aug 30 '15 at 06:53
  • if your script name will not be changed (``script.js``) you will have problems with changing anything - ``script.js`` will be stored in user browser's cache and it won't refresh after you change anything (since name is the same) – Tomasz Madeyski Aug 30 '15 at 06:56
1

Use assetic. In Twig template use:

{% stylesheets '@MyBundle/Resources/public/css/app.css'
        '@MyBundle/Resources/public/css/additional.css'
        output='mycss.css' %}
    <link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

Then use command: php app/console assetic:dump --env=prod

In dev environment you'll see something like http://yoursite.com/09516d4_mycss_1.css but in production will be http://yoursite.com/mycss.css

Note that you must always use assetic:dump when your files was changed to see changes in production.

For fonts I use assetic config:

assetic:
    assets:
        font-awesome-otf:
            inputs: '@AppBundle/Resources/fonts/FontAwesome.otf'
            output: 'fonts/FontAwesome.otf'

You can try the same with images:

assetic:
    assets:
        image-background:
            inputs: '@AppBundle/Resources/images/back.jpg'
            output: 'images/back.jpg'
malcolm
  • 5,486
  • 26
  • 45
  • Great but how should i do for background image urls for example ? Thoses url are stored in css file. I can't put any {% %} in css files... – testpresta Aug 30 '15 at 19:17
  • can you show sample of this in your answer for how the fonts and images are actually used? Like in the css file or html file etc... – Joseph Astrahan Jan 16 '16 at 02:54