我在 Symfony 2 和 MopaBootstrapBundle 中使用较少的 Assets 。
动态生成的 css 文件似乎有效,但未应用样式。如果我们转储 Assets ,那么它确实可以在 prod 中工作,但我不想在 dev 中这样做,因为这违背了我的想法。
config.yml :
assetic:
debug: %kernel.debug%
use_controller: false
bundles: [ MopaBootstrapBundle ]
#java: /usr/bin/java
filters:
cssrewrite: ~
less:
node: /usr/local/bin/node
node_paths: [/usr/local/lib/node_modules]
# auto apply less to all .less files
apply_to: "\.less$"
config_dev.yml
assetic:
use_controller: true
在我的模板中是:
{% stylesheets
'@MopaBootstrapBundle/Resources/public/less/mopabootstrapbundle.less'
%}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" media="screen" />
呈现的 html 是:
<link href="/app_dev.php/css/bab9907_mopabootstrapbundle_1.css" type="text/css" rel="stylesheet" media="screen" />
此文件已解析,我可以将 URL 粘贴到地址栏中并查看有效的 CSS 文件。
此外,如果我们在 prod 中转储 Assets ,它也可以工作
app/console assetic:dump --env=prod
我们清除 prod 缓存并设置输出样式。在开发中,我的样式为零。
我也跑过:
app/console assets:install --symlink web
Installing assets using the symlink option
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
Installing assets for Mopa\Bundle\BootstrapBundle into web/bundles/mopabootstrap
Installing assets for Symfony\Bundle\WebProfilerBundle into web/bundles/webprofiler
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution
所有这些都与 Javascript 相同(在遵循 URL 时文件会解析)
难道我做错了什么?
最佳答案
我也遇到了这个,我也在使用 FosRestBundle并且配置错误:
fos_rest:
format_listener:
rules:
- { path: '^/', priorities: ['json', 'html'], fallback_format: html, prefer_extension: true }
此规则现在适用于对应用程序的每个请求,并且只会知道 json 和 html,并且由于 .css 不匹配它使用 application/json
正确的应该是:
fos_rest:
format_listener:
rules:
- { path: '^/api', priorities: ['json', 'html'], fallback_format: html, prefer_extension: true }
- { path: '^/', priorities: [ 'html', '*/*'], fallback_format: html, prefer_extension: true }
最后一行适用于尚未匹配的所有内容,它告诉/使用内部匹配的内容或类似的智能内容。
瞧
use_controller: true
再次工作
关于less - Symfony 2 开发环境中的 Assets 路径。路径有效,但没有应用样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12115005/