安装composer,在Linux上。
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
第二步:安装完成之后输入
# composer -v
显示如下说明成功。
Do not run Composer as root/super user! See https://getcomposer.org/root for details
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 1.5.2 2017-09-11 16:59:25
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
第二步:接下来进入项目目录
初始化composer
composer init
出现:
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Welcome to the Composer config generator
This command will guide you through creating your composer.json config
Package name (<vendor>/<name>) [root/test]: 输入名字
Description []: 输入描述
Author [, n to skip]:作者信息
Minimum Stability []: stable 资源包的最低稳定版本,默认为 stable
Package Type (e.g. library, project, metapackage, composer-plugin) []: 选择你的类型:库、项目。一般用project
License []: 协议 MIT
Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
{
"name": "mly/test",
"description": "a test",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "mly",
"email": "249005779@qq.com"
}
],
"minimum-stability": "stable",
"require": {}
}
Do you confirm generation [yes]?生成上面的文件? yes
在目录下就生成了composer.json文件
切换到中国镜像,外国网站太慢
composer config repo.packagist composer https://packagist.phpcomposer.com 在composer.json所在目录下执行
接下来
composer install
在当前目录下生成了一个文件夹vendor
vendor目录结构:
vendor----autoload.php
----composer ----autoload_classmap.php
----autoload_namespaces.php
----autoload_psr4.php(自动加载)
----autoload_real.php
----autoload_static.php
----ClassLoader.php
----installed.json
----LICENSE
第三:文件自动加载:例如thinkPHP的function.php的加载
回到项目目录,新建一个common目录,在common下创建function.php文件,随便写个方法。
<?php
function test($str){
return md5($str);
}
然后到项目中,打开composer.json文件,添加红色部分,中括号里面就是要引入文件的路径,需要引入其他文件的时候在中括号中加“,”继续写上路径就可以,之后执行composer dump
{
"name": "mly/test",
"description": "a test",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "mly",
"email": "249005779@qq.com"
}
],
"minimum-stability": "stable",
"require": {},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
"autoload":{
"files":["common/function.php"]
}
}
然后在项目中需要的地方引入vendor下的autoload.php就可以使用。
自动加载类:
{
"name": "mly/test",
"description": "a test",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "mly",
"email": "249005779@qq.com"
}
],
"minimum-stability": "stable",
"require": {},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
"autoload":{
"files":["common/function.php"]//文件的自动加载
"psr-4":{"类的命名空间要双斜杠(App\\Home\\)":"文件路径(相对于composer.json的)"}
}
}
之后运行composer dump
用到的地方引入autoload.php 然后实例化类就可以了(实例化的时候要加上那个类的命名空间或者use那个命名空间)psr-4:类名要和文件名一样
如果要用其他的工具类或者别人写好的类或功能
访问:https://packagist.org/ 可以在里面搜索,选择★高的或者下载量大的可能bug比较少,并且有使用文档。
在项目中运行 composer require +你查找到的类,完成之后到vendor目录下就会看到多了刚才下载的文件
然后在项目中引入autoload.php 实例化就可以用了