0

I just started to use Symfony 3.0.6 framework and I can't understand the using of Assets. I'm trying to add my custom css files to the twig template but it is not working.

I'm running Wamp64 local server and my css is in:

C:\wamp64\www\rating\web\custom.css

rating is the base directory of my project.

I'we done configuration according this page: http://symfony.com/doc/current/cookbook/assetic/asset_management.html#cookbook-assetic-including-css

And here is twig code:

{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('custom.css') }}">
{% endblock %}

When I try to load css from the web it is working without problem.

I can't understand where is the problem. I search web but nothing helped and I'm really mad of it.

2 Answers 2

2

In Symfony, assets are by default added to your app resources or bundle resources. Plainly adding them to your web directory is considered bad practice, because it makes your bundle dependant on your project setup.

In your case, add custom.css to C:\wamp64\www\rating\src\AppBundle\Resources\public\your_subdirectories_here

Then, run the following command: php app/console assets:install --symlink

Symfony will then publish your assets to C:\wamp64\www\rating\web\bundles\app

After doing that, you can access your CSS file using:

<link rel="stylesheet" href="{{ asset('bundles/app/your_subdirectories_here/custom.css') }}">
13
  • Thanks for answer, but the basic problem is that custom.css is not loaded to the browser.
    – sakonn
    May 25, 2016 at 9:58
  • Forgot to add that part, apologies. Re-check the answer :) Also, you might find this article interesting: symfony.com/doc/current/best_practices/web-assets.html
    – Richard
    May 25, 2016 at 10:02
  • As I write earlier I don't know where the problem is but my css still isn't loaded to the browser. Even after following your advice.
    – sakonn
    May 25, 2016 at 10:08
  • Did your CSS file end up in web/bundles/app after executing php app/console assets:install --symlink? If so, the provided twig code should work (after modifying the your_subdirectories_here part of course). If not, check if custom.css is added to src/AppBundle/public.
    – Richard
    May 25, 2016 at 10:10
  • Yes the full path is now: C:\wamp64\www\rating\web\bundles\app\custom.css And my Twig command: <link rel="stylesheet" href="{{ asset('bundles/app/custom.css') }}">
    – sakonn
    May 25, 2016 at 10:11
0

(Posted on behalf of the OP):

I found where the problem was. When I was configuring my local alias (rating:8080) I added this path

<VirtualHost *:8080>
  DocumentRoot "c:/wamp64/www/rating/web/app_dev.php"
  ServerName rating
</VirtualHost>

instead of

<VirtualHost *:8080>
  DocumentRoot "c:/wamp64/www/rating/web"
  ServerName rating
</VirtualHost>

So when I call rating:8080 from my browser it goes directly to php and not to .htacces. As result the route wasn't configured what is logic :)

As last I have to configure my .htacces file because the default redirecting was to app.php and not app_dev.php (simply every where inside the file change the app.php to app_dev.php).

Now everything works perfectly.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.