我已经使用 Composer 安装了 jQuery。
现在 jQuery 位于 vendor/components/jquery
目录中。
我尝试将其包含在 /app/config/config.yml
assetic:
debug: "%kernel.debug%"
use_controller: false
filters:
cssrewrite: ~
assets:
jquery: %kernel.root_dir%/../vendor/components/jquery/jquery.min.js
如果我运行命令:
php app/console assetic:dump
我明白
[RuntimeException]
The source file "/home/user/project/app/current/app/../web/vendor/components/jquery/jquery.min.js" does not exist.
因为 assetic 仍在 /web/
目录中查找,这是错误的。如何更改它以便它可以在 /vendor/
目录中查找?另外,我不想将 jquery 文件放在公共(public)捆绑文件夹中,因为这会破坏通过 Composer 获取正确版本的供应商库的所有感觉。
最佳答案
我不太喜欢在 Symfony 的供应商目录中安装 jquery,因为它不是 PHP 库。 Composer 和vendor
目录只能用于 PHP 依赖项。
您应该将前端部分与后端部分分开。
所有 js、css 和其他资源都应该放在其他地方。
因此你有两个选择:
- 如果您希望每个 bundle 拥有单独的资源,请将用于前端的资源放入 bundle 的公共(public)目录中:例如,将您的
jquery.min.js
在Resources/public/js
您的 bundle 的文件夹。
然后运行命令php app/console assets:install
。这会将您的 js 文件复制到 web/bundles/yourbundle/js/
目录。
最后使用<script type="text/javascript" src="{{ asset('yourbundle/js/jquery.min.js') }}"></script>
在你的 twig 文件中添加 jquery。
- 如果您的所有 bundle 都有通用资源,请将您的资源直接放在
web
中文件夹,例如:web/js
,web/css
,web/img
... 然后使用<script type="text/javascript" src="{{ asset('js/jquery.min.js') }}"></script>
在你的 Twig 模板中。
关于php - 使用 Composer 和 Assets 将 jQuery 包含在 symfony 项目中的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38654696/