laravel 如何将composer的vendor文件夹中的CSS和JS文件复制到public?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34387813/
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
How to copy the CSS and JS files in vendor folder of composer to public?
提问by Dylan
I'm just starting to learn several PHP frameworks. The examples I tried all use composer
, which I have not used before, so things are not exactly clear to me yet.
我刚刚开始学习几个 PHP 框架。我尝试的所有示例都使用了composer
我以前没有使用过的,所以我还不太清楚。
I specified some JS and CSS libraries in composer.json
and ran composer install
.
Now these libraries are added to the vendor
folder.
我在中指定了一些 JS 和 CSS 库composer.json
并运行了composer install
. 现在这些库被添加到vendor
文件夹中。
Now how am I supposed to use the JS and CSS files that are in those folders, for example bootstrap/dist/css/bootstrap.min.css
?
The examples I tried either just have a copy in the public/css
folder or use a remote link, like <link href="//netdna.bootstrapcdn.com/bootswatch/2.3.1/united/bootstrap.min.css" rel="stylesheet">
in a volt
-file.
例如,现在我应该如何使用这些文件夹中的 JS 和 CSS 文件bootstrap/dist/css/bootstrap.min.css
?我试过的例子要么在public/css
文件夹中有一个副本,要么使用远程链接,比如<link href="//netdna.bootstrapcdn.com/bootswatch/2.3.1/united/bootstrap.min.css" rel="stylesheet">
在volt
-file.txt 中。
For some reason I don't like remote links like these in my application (I'm afraid they could be 'down' some day).
But the other option is not very beautiful as well, I think, since I still have to pick the right files to copy from vendor
to public
.
出于某种原因,我不喜欢我的应用程序中像这样的远程链接(恐怕有一天它们可能会“关闭”)。但另一种选择是不是很漂亮,以及,我想,既然我还是要选择正确的文件从复制vendor
到public
。
Isn't there an 'automatic' way to do this ? Or maybe by specifying it into a controller, which then copies the files it into public
?
没有一种“自动”的方法可以做到这一点吗?或者也许通过将它指定到控制器中,然后将文件复制到public
?
回答by Jens A. Koch
Composer was written to fetch PHP dependencies. It doesn't play well with other web assets, like CSS or JS files.
编写 Composer 是为了获取 PHP 依赖项。它不适用于其他 Web 资产,例如 CSS 或 JS 文件。
Composer Asset Plugin
作曲家资产插件
To address the problem of fetching assets a Composer Asset Plugin was developed by Fran?ois Pluchino (fxp). It provides a NPM/Bower dependency manager for Composer. Its a bridge to these dependency managers and will fetch the stuff into the vendor folder.
为了解决获取资源的问题,Fran?ois Pluchino (fxp) 开发了 Composer Asset Plugin。它为 Composer 提供了一个 NPM/Bower 依赖管理器。它是连接这些依赖项管理器的桥梁,并将把这些东西提取到供应商文件夹中。
Keep in mind that you can always use Bower or NPM directly. Fetch your PHP dependencies with Composer defined in composer.json. Fetch your assets with Bower defined in bower.json. It might be a cleaner approach.
请记住,您始终可以直接使用 Bower 或 NPM。使用 composer.json 中定义的 Composer 获取您的 PHP 依赖项。使用 bower.json 中定义的 Bower 获取您的资产。这可能是一种更清洁的方法。
You might require it from the CLI: composer require fxp/composer-asset-plugin
您可能需要从 CLI 获取它: composer require fxp/composer-asset-plugin
For example, to fetch the assets "Twitters Bootstrap" and "Jquery":
例如,要获取资产“Twitters Bootstrap”和“Jquery”:
{
"require": {
"bower-asset/bootstrap": "dev-master",
"bower-asset/jquery-2.1.0": "2.1.0"
}
}
Some frameworks provide custom handlers for assets, mainly to support their individual folder layout and copy things into the correct places from the vendor folder. I don't know if Phalcon has something to fetch assets.
一些框架为资产提供自定义处理程序,主要是为了支持它们各自的文件夹布局并将内容从供应商文件夹复制到正确的位置。不知道 Phalcon 有没有什么可以获取资源的。
Isn't there an 'automatic' way to do this ? Or maybe by specifying it into a controller, which then copies the files it into public ?
没有一种“自动”的方法可以做到这一点吗?或者也许通过将它指定到一个控制器中,然后将它的文件复制到 public 中?
Copying assetsfrom the vendor folder into the correct spot in your application layout is another story.
将资源从供应商文件夹复制到应用程序布局中的正确位置是另一回事。
There is no automatic way of doing this. You have to provide the mechanism yourself.
没有自动执行此操作的方法。您必须自己提供机制。
For instance, you might alter the installation path from vendor to public/assets
or something,
see https://github.com/francoispluchino/composer-asset-plugin/blob/master/Resources/doc/index.md#define-a-custom-directory-for-the-assets-installation
例如,您可能会更改从供应商到public/assets
其他安装路径,请参阅https://github.com/francoispluchino/composer-asset-plugin/blob/master/Resources/doc/index.md#define-a-custom-资产安装目录
The plugin doesn't copy things around.
该插件不会复制周围的东西。
Bower + Grunt(brief: fetch assets with Bower, copy stuff around with Grunt)
Bower + Grunt(简要:使用 Bower 获取资源,使用 Grunt 复制内容)
The closest to "specifying a controller" would be to setup a Grunttask to copy only relevant stuff fetch with Bower from web/assets/vendor
to web/assets/app
folder.
最接近“指定控制器”的方法是设置一个Grunt任务,以仅将使用 Bower 获取的相关内容复制web/assets/vendor
到web/assets/app
文件夹中。
In other words: not even a JS package manager like Bower provides automatic copying of main API files. Bower would fetch the latest jQuery version into a defined vendor folder, but it won't automatically copy jquery.min.js
into web/assets/js/jquery.min.js
. You need a task on top of it.
换句话说:即使是像 Bower 这样的 JS 包管理器也没有提供主 API 文件的自动复制。Bower 会将最新的 jQuery 版本提取到定义的供应商文件夹中,但不会自动复制jquery.min.js
到web/assets/js/jquery.min.js
. 您需要在其之上执行一项任务。