composer简介

来源:互联网 发布:win7电脑桌面美化软件 编辑:程序博客网 时间:2024/06/07 17:58

简介

composer是一款php依赖包管理工具,使得你不必像以前一样手动去下载一些依赖项目并进行复杂的依赖处理,composer只需要声明一个json文件,便可以自动下载管理依赖包,只需要简单的声明依赖文件然后通过几条composer命令便可以自动安装,更新,删除,搜索等对依赖的管理,composer将会自动从服务器中下载依赖包,并生成依赖文件。

安装

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

上面的命令将会下载一个名为composer.php的文件,一个上万行的单个文件,这便是composer的代码。下面我们把composer移动到系统可执行文件搜索路径下面,方面我们后续直接使用“composer xxx”的命令形式,而不是“php xxxxx/composer.php xxx“的形式。

mv composer.phar /usr/local/bin/composer

简单的使用例子

下面我们先看看composer的基本使用,我们将会以如何引用一个叫macaw的路由框架来介绍composer的用法。

创建项目

创建一个空目录

mkdir HelloComposer

声明HelloComposer项目所依赖的项目,把以下内容写入到composer.json文件中

require: {    "noahbuscher/macaw": "dev-master"}

声明好依赖的项目之后,执行一条命令就可以安装依赖库了:

composer install

现在macaw已经被安装在vendor目录中了,可以通过include composer自动生成的一个名文autoload.php的文件我们就可以引用所有的依赖项目了:
创建index.php文件:

require('vendor/autoload.php');use \NoahBuscher\Macaw\Macaw;Macaw::get('/', 'Controllers\demo@index');Macaw::get('page', 'Controllers\demo@page');Macaw::get('view/(:num)', 'Controllers\demo@view');Macaw::dispatch();

创建demo.php文件:

<?phpnamespace controllers;class Demo {    public function index()    {        echo 'home';    }    public function page()    {        echo 'page';    }    public function view($id)    {        echo $id;    }}

然后就可以通过访问index.php看看情况了
如果需要安装其它依赖包,只要在composer.json文件里面增加相应项目即可

composer命令列表

Available commands:  about           Short information about Composer.  archive         Create an archive of this composer package.  browse          Opens the package's repository URL or homepage in your browser.  clear-cache     Clears composer's internal package cache.  clearcache      Clears composer's internal package cache.  config          Set config options.  create-project  Create new project from a package into given directory.  depends         Shows which packages cause the given package to be installed.  diagnose        Diagnoses the system to identify common errors.  dump-autoload   Dumps the autoloader.  dumpautoload    Dumps the autoloader.  exec            Execute a vendored binary/script.  global          Allows running commands in the global composer dir ($COMPOSER_HOME).  help            Displays help for a command  home            Opens the package's repository URL or homepage in your browser.  info            Show information about packages.  init            Creates a basic composer.json file in current directory.  install         Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.  licenses        Show information about licenses of dependencies.  list            Lists commands  outdated        Shows a list of installed packages that have updates available, including their latest version.  prohibits       Shows which packages prevent the given package from being installed.  remove          Removes a package from the require or require-dev.  require         Adds required packages to your composer.json and installs them.  run-script      Run the scripts defined in composer.json.  search          Search for packages.  self-update     Updates composer.phar to the latest version.  selfupdate      Updates composer.phar to the latest version.  show            Show information about packages.  status          Show a list of locally modified packages.  suggests        Show package suggestions.  update          Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.  validate        Validates a composer.json and composer.lock.  why             Shows which packages cause the given package to be installed.  why-not         Shows which packages prevent the given package from being installed.
原创粉丝点击