筹备
- Github账号
- packagist账号
- 装置好git
- 装置好composer
步骤
创立并拉取一个仓库
Github右上角+号,点 New repository。创立实现后应用git拉取到本地。
composer初始化
主动创立
应用composer init
命令进行初始化
composer init
composer init Welcome to the Composer config generator 欢送来到Composer配置生成器This command will guide you through creating your composer.json config. 这个命令将领导您创立编写器。json配置。Package name (<vendor>/<name>) [z/lattice-php]: // 你的包名,不能够反复Description []: // 你的形容Author [Chenilove <2665468087@qq.com>, n to skip]: // 作者Minimum Stability []: // 版本稳定性 例如 dev stable 等Package Type (e.g. library, project, metapackage, composer-plugin) []: // 包类型License []: MIT // 协定Define your dependencies.Would you like to define your dependencies (require) interactively [yes]? 你想交互式地定义你的依赖关系吗Search for a package:Would you like to define your dev dependencies (require-dev) interactively [yes]? 您想交互式地定义您的开发依赖项(require-dev)吗Search for a package:Add PSR-4 autoload mapping? Maps namespace "Z\LatticePhp" to the entered relative path. [src/, n to skip]: 增加PSR-4主动加载映射?将命名空间“Z\LatticePhp”映射到输出的相对路径。{ "name": "z/lattice-php", "license": "MIT", "autoload": { "psr-4": { "Z\\LatticePhp\\": "src/" } }, "authors": [ { "name": "Chenilove", "email": "2665468087@qq.com" } ], "require": {}}Do you confirm generation [yes]? 确定生成吗?Generating autoload filesGenerated autoload filesPSR-4 autoloading configured. Use "namespace Z\LatticePhp;" in src/Include the Composer autoloader with: require 'vendor/autoload.php';
手动创立
实质上composer init
命令是在根目录创立一个composer.json
,咱们齐全能够本人手动创立
{ "name": "zmxy/lattice", "description": "PHP生成点阵图组件.教程:https://github.com/Chenilove/LatticePHP", "license": "MIT", "autoload": { "psr-4": { "Lattice\\": "src/" } }, "authors": [ { "name": "Chenilove", "email": "2665468087@qq.com" } ], "minimum-stability": "stable", "require": { "php": ">=7.1.0" }}
autoload主动加载
autoload
这个选项很重要,这里规定了命名空间。假如你想再加一个命名空间,你能够这样做
"autoload": { "psr-4": { "Lattice\\": "src/" 解释: "命名空间\\": "包的相对路径", } },
每次批改得应用命令刷新
composer dump-autoload
创立代码
这里对应下面的autoload->psr-4
外面的配置,先创立一个src
目录,而后在外面写你的代码。
namespace Lattice;/** * Class Lattice */class Lattice{ public function index() { echo "嘿嘿嘿"; }}
提交代码到Github
应用git提交代码到Github仓库
提交到packagist
- 先复制Github仓库地址(例如:https://github.com/Chenilove/...)
- 关上packagist,点击右上角的Submit
- 将仓库地址填入
Repository URL
,而后点击Check - 最初回到Github,Release一个版本,packagist就能自动更新
- 到此曾经胜利公布了一个composer包,每次Release一个版本时,packagist都会自动更新。