Note: Although display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">2.0-beta .1 Strictly speaking, it is earlier than display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">2.0, however, according to version constraints,
For example, display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">~1.2 will not install this version. As mentioned before display: inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">~1.2 only means display:inline-block; line-height:19.6875px; margin: 0px 2px; background-color:rgb(249,242,244)">.2 part can be changed, but display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)" >1. part is fixed.
稳定性默认情况下只有稳定的发行版才会被考虑在内。如果你也想获得 RC、beta、alpha 或 dev 版本,你可以使用 稳定标志。你可以对所有的包做 最小稳定性 设置,而不是每个依赖逐一设置。安装依赖包获取定义的依赖到你的本地项目,只需要调用 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.phar 运行 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">install 命令。php composer.phar install
接着前面的例子,这将会找到 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">monolog/monolog 的最新版本,并将它下载到 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor 目录。
这是一个惯例把第三方的代码到一个指定的目录 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor。如果是
monolog 将会创建 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor/monolog/monolog 目录。小技巧: 如果你正在使用Git来管理你的项目, 你可能要添加 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">vendor 到你的 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">.Gitignore 文件中。
你不会希望将所有的代码都添加到你的版本库中。
另一件事是 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">install 命令将创建一个 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.lock 文件到你项目的根目录中。display:inline-block; line-height:33.75px; margin:0px 2px; background-color:rgb(249,242,244)">composer.lock -
Lock fileAfter installing dependencies, Composer will write the exact version number list during installation display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)"> composer.lock file. This will lock the specific version of your project. Please submit display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244 )">composer.lock (including display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json) to your repository This is very important because the display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">install command will check whether the lock file exists, if exists, it will download the specified version (ignoring the definitions in the display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json file). This means that anyone building the project will download the exact same dependency as the specified version. Your continuous integration server, production environment, other developers on your team, everything and everyone will use the same dependency. Mitigate the impact of potential bugs on your deployment. Even if you develop the project alone, you can continue to work with confidence when you reinstall the project in six months, even if many new versions of your dependencies have been released since then If it does not exist display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.lock file, Composer
Will read display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json and create the lock file.这意味着如果你的依赖更新了新的版本,你将不会获得任何更新。此时要更新你的依赖版本请使用 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">update 命令。这将获取最新匹配的版本(根据你的 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json 文件)并将新版本更新进锁文件。php composer.phar update
如果只想安装或更新一个依赖,你可以白名单它们:php composer.phar update monolog/monolog [...]
注意: 对于库,并不一定建议提交锁文件 请参考:库的锁文件.
Packagistpackagist 是 Composer 的主要资源库。 一个 Composer 的库基本上是一个包的源:记录了可以得到包的地方。Packagist 的目标是成为大家使用库资源的中央存储平台。这意味着你可以 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">require 那里的任何包。当你访问 packagist website (packagist.org),你可以浏览和搜索资源包。任何支持 Composer 的开源项目应该发布自己的包在 packagist 上。虽然并不一定要发布在 packagist 上来使用 Composer,但它使我们的编程生活更加轻松。自动加载对于库的自动加载信息,Composer 生成了一个 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor/autoload.php 文件。你可以简单的引入这个文件,你会得到一个免费的自动加载支持。require'vendor/autoload.php';这使得你可以很容易的使用第三方代码。例如:如果你的项目依赖 monolog,你就可以像这样开始使用这个类库,并且他们将被自动加载。$log=newMonolog\Logger('name');$log->pushHandler(newMonolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));$log->addWarning('Foo');你可以在 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json 的 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">autoload 字段中增加自己的
autoloader。{
"autoload": {
"psr-4": {"Acme\\": "src/"}
}
}
Composer 将注册一个 PSR-4 autoloader 到 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">Acme 命名空间。你可以定义一个从命名空间到目录的映射。此时 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">src 会在你项目的根目录,与 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor 文件夹同级。例如 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">src/Foo.php 文件应该包含 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">Acme\Foo 类。添加 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">autoload 字段后,你应该再次运行 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">install 命令来生成 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor/autoload.php 文件。引用这个文件也将返回 autoloader 的实例,你可以将包含调用的返回值存储在变量中,并添加更多的命名空间。这对于在一个测试套件中自动加载类文件是非常有用的,例如。$loader=require'vendor/autoload.php';$loader->add('Acme\\Test\\',__DIR__);除了 PSR-4 自动加载,classmap 也是支持的。这允许类被自动加载,即使不符合 PSR-0 规范。详细请查看 自动加载-参考。Note: Composer provides its own autoloader. If you don't want to use it, you can just include the display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">vendor/composer/autoload_*.php files , it returns an associative array through which you can configure your own
autoloader.
The above introduces the installation and use of PHP Composer, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.