Home > Backend Development > PHP Tutorial > PHP 的一个依赖管理工具Composer

PHP 的一个依赖管理工具Composer

WBOY
Release: 2016-06-20 12:30:17
Original
1097 people have browsed it

1:下载

curl -sS https://getcomposer.org/installer | php
Copy after login


或者

php -r "readfile('https://getcomposer.org/installer');" | php
Copy after login



2:移动到bin目录下,以后每次使用直接使用composer命令

mv composer.phar /usr/local/bin/composer
Copy after login

查看版本

composer -V
Copy after login



3.composer.json 在项目中安装monolog
composer.json文件内容如下

{    "require": {        "monolog/monolog": "1.0.*"    }}
Copy after login

使用composer install 安装,在当前目录下回创建vendor/monolog/monolog 目录
Composer 生成了一个 vendor/autoload.php 文件。你可以简单的引入这个文件,你会得到一个免费的自动加载支持。
这使得你可以很容易的使用第三方代码。项目依赖 monolog,你就可以像这样开始使用这个类库,并且他们将被自动加载。

$log = new Monolog\Logger('name');$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));$log->addWarning('Foo');
Copy after login

4:下载thinkphp框架并安装monolog

composer create-project topthink/thinkphp myProjectNamecd myProjectNamevim composer.json
Copy after login

找到require
修改为:

    "require": {        "php": ">=5.3.0",         "monolog/monolog": "1.0.*"    },    找到http://packagist.phpcomposer.com改为https://packagist.phpcomposer.com
Copy after login

保存退出

composer install
Copy after login


当前目录会
在当前目录下回创建vendor/monolog/monolog 目录
可在框架入口文件中引入 vendor/autoload.php
就可在框架中使用了

5.获取依赖的最新版本

    composer update
Copy after login


如果你只是想更新几个包

    composer update vendor/package vendor/package2
Copy after login


用通配符进行批量更新

    composer update vendor/*
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template