Symfony Unable to Generate a URL for Route to Stylesheet

ε祈祈猫儿з 提交于 2020-02-03 18:57:11

问题


I have two CSS files in the same folder, with identical access rights. When testing in the same Twig file, one CSS file generates a URL to the file and loads perfectly and one gives an error.

Error

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "_assetic_a328b4c_0" as such route does not exist.") in @GutensiteStream/Stream35163.html.twig at line 19.

Files on Server

ls -al /var/www/core/cms/src/Templates/GutensiteAdminBundle/Resources/public/css
-rw-rw-r-- 1 chadwick developer  17K Feb  7 14:00 dashboard.css
-rw-rw-r-- 1 chadwick developer  49K Feb  6 16:00 site.css

Template with CSS that Loads

{% stylesheets '@TemplatesGutensiteAdminBundle/Resources/public/css/site.css' %}
<link rel="stylesheet" href="{{ asset_url }}">
{% endstylesheets %}

Template with CSS that does NOT Load

{% stylesheets '@TemplatesGutensiteAdminBundle/Resources/public/css/dashboard.css' %}
<link rel="stylesheet" href="{{ asset_url }}">
{% endstylesheets %}

Linking to the File Directly Works

<link rel="stylesheet" href="/gutensite/v/2/0/bundles/templatesgutensiteadmin/css/dashboard.css">

Steps Taken

I have already cleared cache multiple times, via app/console cache:clear and via rm -rf app/cache/*. I use php app/console assets:install --symlink /var/www/core/web/gutensite/v/2/0/ to symlink the public files to the bundles, and of course they are accessible (as demonstrated via the direct link).

My Config.yml

assetic:
    #don't use dynamic links to assets, use full production paths
    use_controller: false

回答1:


When I ran into this error, the solution was quite simple:

When in doubt:

bin/console cache:clear

Assetic is looked for a cached version which doesn't exist. This could be for any number of reasons, but cache:clear seems like it should be the first stop.




回答2:


It is not recommended to use the @ helper and load css directly from your bundle in production.

The best practice approach is to store the css files in the app/Resources directory and using assets:install requesting them from the web root, but seing as you don't have an AppBundle, the second best thing would be to request them from the web/bundles directory:

    {% stylesheets
    'bundles/templategutenstideadmin/css/site.css'
    'bundles/templategutenstideadmin/css/dashboard.css'
    %}
    <link rel="stylesheet" href="{{ asset_url }}">
    {% endstylesheets %}

Make sure you run assets:install beforehand. This way, your css files will be compressed to one file and they will be loaded with the latest changes from the assets:install command.

Loading css files directly from your bundle using @ is not recommended outside of local testing. I suspect your first issue is the usage of muliple stylesheets tags in assetic (stylesheets tag is not a part of Twig), but generally using @ for file linking is not a good idea for assets. Use it for things like routing and config in .yml files, but not for assets! :)




回答3:


One instance where this error will occur is when you try to load such an asset in a child template, that is already being loaded in one of the parent templates.

To fix it, load the asset only once in a template chain.

(Have to post this as answer, as I can't comment, yet.)




回答4:


config.yml

assetic:
    bundles: [you bundle name , otherbundlename]  #add this


来源:https://stackoverflow.com/questions/28388716/symfony-unable-to-generate-a-url-for-route-to-stylesheet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!