如何让 Symfony2 直接加载 CSS、JS 文件而不是通过 PHP?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8258294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:17:44  来源:igfitidea点击:

How to make Symfony2 to load CSS, JS files directly and not via PHP?

phpsymfony

提问by Tower

OLD QUESTION, SEE BELOW FOR THE UPDATED VERSION

老问题,请参阅下面的更新版本

My development environment is not the fastest. I takes roughly 500ms per PHP request. It's starting to become a problem with Symfony2 resource files because each of the resource files are being requested via Symfony's internal controllers:

我的开发环境不是最快的。我每个 PHP 请求大约需要 500 毫秒。它开始成为 Symfony2 资源文件的问题,因为每个资源文件都是通过 Symfony 的内部控制器请求的:

http://localhost/myproj/app_dev.php/js/bb8690a_part_4_myJavaScriptFile_2.js

http://localhost/myproj/app_dev.php/js/bb8690a_part_4_myJavaScriptFile_2.js

As can be seen, the files are loaded via the Symfony framework and not directly. Since I'm starting to have over 20 files to load, multiplying that with the 500ms makes page loads very slow. I want to load the files directly, but I am not sure how to do that.

可以看出,文件是通过 Symfony 框架而不是直接加载的。由于我开始要加载 20 多个文件,因此将其与 500 毫秒相乘会使页面加载速度非常慢。我想直接加载文件,但我不知道该怎么做。

This is part of the config.yml:

这是以下内容的一部分config.yml

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    # java: /usr/bin/java
    filters:
        cssrewrite: ~

I thought setting use_controllerto falsewould do it, but nope.

我以为设置use_controllerfalse会做,但没了。

Is there a way to handle the loading of those resoures directly?

有没有办法直接处理这些资源的加载?

UPDATE:

更新:

This is the URL it tries to use now:

这是它现在尝试使用的 URL:

http://localhost/myproj/_controller/js/bb8690a_part_4_myJavaScriptFile_2.js

http://localhost/myproj/_controller/js/bb8690a_part_4_myJavaScriptFile_2.js

I have set use_controllerto falsefor both dev and general configs. How do I get rid of that _controllerpart of the URL?

我已use_controllerfalse了既开发和一般CONFIGS。我如何摆脱_controllerURL的那部分?

Edit: If I clear the cache, run assetic:dumpand have use_controlleras false, then upon reload I get Cannot load resource ".". I can't get around that problem unless I temporarily enable use_controllerfor one page load. After that, I disable it and reload and now it requests from that invalid URL that contains _controller.

编辑:如果我清除缓存,运行assetic:dump并拥有use_controlleras false,然后在重新加载时我得到Cannot load resource ".". 除非我暂时启用use_controller一页加载,否则我无法解决这个问题。之后,我禁用它并重新加载,现在它从包含_controller.

It also seems to work in prod, but not in dev. Strange.

它似乎也适用于生产,但不适用于开发。奇怪的。

Template code:

模板代码:

{% stylesheets filter="cssrewrite"
            'bundles/outotecofil/css/reset.css'
            'bundles/outotecofil/css/*'

            output='css/dist/dist.css'
        %}
        <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}

        {% javascripts
            '@OutotecCommonBundle/Resources/public/js/jquery-1.6.2.min.js'
            '@OutotecCommonBundle/Resources/public/js/jquery-ui-1.8.16.custom.min.js'
            '@OutotecCommonBundle/Resources/public/js/chosen.jquery.min.js'
            '@OutotecCommonBundle/Resources/public/js/widget/*'

            '@OutotecOFILBundle/Resources/public/js/OFILDependencyManager.js'
            '@OutotecOFILBundle/Resources/public/js/widget/*'
            '@OutotecOFILBundle/Resources/public/js/plant-scope.js'

            output='js/dist/dist.js'
        %}
        <script src="{{ asset_url }}"></script>
        {% endjavascripts %}

To be extremely clear: without app_dev.php (i.e. in prod mode), it works. Only in dev it does not and throws this "Cannot load resource "."" error unless I first enable use_controllerfor one request, after which I can disable it and reload though the URLs will then contain _controller/in their paths.

非常清楚:没有 app_dev.php(即在 prod 模式下),它可以工作。只有在开发中它不会并抛出这个“无法加载资源”。“”错误,除非我首先启用use_controller一个请求,之后我可以禁用它并重新加载,尽管 URL 将包含_controller/在它们的路径中。

回答by ArthurM

Try to remove this part of code in routing_dev.yml when use_controller is false :

当 use_controller 为 false 时,尝试删除 routing_dev.yml 中的这部分代码:

_assetic:
    resource: .
    type:     assetic

回答by dlondero

Symfony documentation is always the first place where to start look: How to Use Assetic for Asset Management

Symfony 文档始终是开始查看的第一个地方:如何使用 Assetic 进行资产管理

In the prod environment, your JS and CSS files are represented by a single tag each. In other words, instead of seeing each JavaScript file you're including in your source, you'll likely just see something like this:

在 prod 环境中,您的 JS 和 CSS 文件分别由一个标签表示。换句话说,您不会看到源代码中包含的每个 JavaScript 文件,而只会看到如下内容:

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

Moreover, that file does not actually exist, nor is it dynamically rendered by Symfony (as the asset files are in the dev environment). This is on purpose - letting Symfony generate these files dynamically in a production environment is just too slow.

此外,该文件实际上并不存在,也不是由 Symfony 动态呈现的(因为资产文件在开发环境中)。这是故意的 - 让 Symfony 在生产环境中动态生成这些文件太慢了。

Instead, each time you use your app in the prod environment (and therefore, each time you deploy), you should run the following task:

相反,每次您在 prod 环境中使用您的应用程序时(因此,每次部署时),您都应该运行以下任务:

php app/console assetic:dump --env=prod --no-debug

This will physically generate and write each file that you need (e.g. /js/abcd123.js). If you update any of your assets, you'll need to run this again to regenerate the file.

这将物理生成并写入您需要的每个文件(例如 /js/abcd123.js)。如果您更新了任何资产,则需要再次运行以重新生成文件。

回答by five

If you use assetic:dump, then you have to cache:clear -e dev

如果你使用assetic:dump,那么你必须cache:clear -e dev

"...if you run cache:clear on your production cache, it warms up the cache with debug mode on. If you try to dump assets afterwards, weird things might happen."

“...如果你在你的生产缓存上运行 cache:clear,它会在调试模式下预热缓存。如果你之后尝试转储资产,可能会发生奇怪的事情。”

i found it here: http://sftuts.com/using-assetic-in-symfony2-for-css-compression(4. paragraph)

我在这里找到它:http: //sftuts.com/using-assetic-in-symfony2-for-css-compression(4.段落)

回答by gagarine

From the documentation

文档

Modify the devconfig to avoid using the controller.

修改开发配置以避免使用控制器。

# app/config/config_dev.yml
assetic:
    use_controller: false

Remove the route in routing_dev.yml to avoid side effect

删除routing_dev.yml中的路由以避免副作用

# app/config/routing_dev.yml
_assetic:
    resource: .
    type:     assetic

Automatically dump your css/less files every time you have a modification.

每次修改时自动转储 css/less 文件。

php app/console assetic:dump --watch

回答by Karol Sikora

I have the same problem, working configuration is: comment out from routing_dev.yml:

我有同样的问题,工作配置是:从routing_dev.yml中注释掉:

_assetic:
 resource: .
 type:     assetic

set use_controller to false. After doing this I'm able to use assetic:dump and see working page.

将 use_controller 设置为 false。完成此操作后,我可以使用 assetic:dump 并查看工作页面。