Composer
、NPM
和Bower
三种依赖管理器。其中,Composer
主要用于PHP依赖管理,通过composer.json
文件来确定需要安装的包。你可以通过修改该文件或者运行php composer install <package>
来安装所需的包。而NPM
则主要用于JavaScript依赖管理,并且拥有大量的包。通过运行npm install
命令来安装依赖,其安装的包由package.json
文件决定。至于Bower
,据我所知它主要用于前端包管理。在Laravel中,你可以任意使用这三个工具。但是,在某些情况下,比如一个库既可以通过
npm
也可以通过composer
来获取,你应该选择哪一个呢?举个例子,在Laravel的安装中,有两个文件:
app.js
(应用程序的主JS文件)和bootstrap.js
(在app.js中引入一些依赖项的文件)。下面是
resources/js
目录下我的bootstrap.js
文件的内容:window._ = require('lodash');
window.Popper = require('popper.js').default;
/**
* We'll load jQuery and the Bootstrap jQuery plugin which provides support
* for JavaScript based Bootstrap features such as modals and tabs. This
* code may be modified to fit the specific needs of your application.
*/
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap');
require('slick-carousel');
require('isotope-layout/dist/isotope.pkgd.min.js');
require('tablesorter/dist/js/jquery.tablesorter.combined.min.js');
}
catch (e) {}
/**
* We'll load the axios HTTP library which allows us to easily issue requests
* to our Laravel back-end. This library automatically handles sending the
* CSRF token as a header based on the value of the "XSRF" token cookie.
*/
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
/**
* Next we will register the CSRF Token as a common header with Axios so that
* all outgoing HTTP requests automatically have it attached. This is just
* a simple convenience so we don't have to attach every token manually.
*/
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
/**
* Echo exposes an expressive API for subscribing to channels and listening
* for events that are broadcast by Laravel. Echo and event broadcasting
* allows your team to easily build robust real-time web applications.
*/
// import Echo from 'laravel-echo'
// window.Pusher = require('pusher-js');
// window.Echo = new Echo({
// broadcaster: 'pusher',
// key: process.env.MIX_PUSHER_APP_KEY,
// cluster: process.env.MIX_PUSHER_APP_CLUSTER,
// encrypted: true
// });
在
app.css
文件中,我有以下内容:/*
* This file takes all of the styling we need and compiles it into one nice CSS file.
* You'll notice you can pull in anything from the node_modules folder use a Tiddle (~)
*/
@import '~bootstrap/dist/css/bootstrap.min.css'; // Bootstrap 3.3.7 CSS
@import '~slick-carousel/slick/slick.css'; // Slick Carousel base CSS
@import "variables"; // Sass Variables
@import "partials/typography"; // All from this point are from the partials folder
@import "partials/mixins";
@import "partials/helpers";
@import "partials/navigation";
@import "partials/breadcrumb-bar";
@import "partials/welcome-box";
@import "partials/form-box";
@import "partials/content-box";
@import "partials/carousels";
@import "partials/tables";
@import "partials/interactions-row";
@import "partials/downloads-area";
@import "partials/articles-events";
@import "partials/biography-pages";
@import "partials/grid";
@import "partials/footer";
@import "partials/steve-custom.scss";
我真正困扰的是:为什么
app.js
知道我正在引用 node_modules
文件夹中的文件,而 app.css
只需使用 ~
就知道我正在引用 Bootstrap?为什么我不需要指定绝对路径?一般来说,JavaScript 相关的项目通常来自于
npm
,而 PHP 依赖项则来自于 composer
。但是,我对 Laravel Full Calendar 包感到困惑,因为它的样式和 JS 代码似乎是通过 npm 获取的,但其 PHP 依赖部分却是通过 Composer 获取的。这种情况正常吗?我知道有很多问题,但我觉得 Laracasts 没有很好地解释这些包管理器的实际用法。
.js
和.css
文件。 - Adam