PHP:发布一个composer包到Packagist
【摘要】
more-echo-php
a demo for PHP composer
一个PHP的composer包发布示例
1、初始化项目
mkdir more-echo-php
cd more-ech...
more-echo-php
a demo for PHP composer
一个PHP的composer包发布示例
1、初始化项目
mkdir more-echo-php
cd more-echo-php
# 初始化项目, 填写好项目的信息
$ composer init
- 1
- 2
- 3
- 4
- 5
- 6
项目目录
$ tree -I vendor
.
├── README.md
├── composer.json
└── src
└── EchoText.php
- 1
- 2
- 3
- 4
- 5
- 6
- 7
composer.json
{
"name": "mouday/more-echo",
"description": "a demo for Composer",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Mouday\\MoreEcho\\": "src/"
}
},
"authors": [
{
"name": "pengshiyu",
"email": "1940607002@qq.com"
}
],
"require": {}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
src/EchoText.php
<?php
namespace Mouday\MoreEcho;
/**
* a demo for php composer
* Class EchoText
* @package Mouday\MoreEcho
*/
class EchoText
{
public static function echoText($text)
{
echo $text;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
发布项目
- 将项目提交到github
- 将项目发布到packagist,点击submit输入github的项目地址
发布包地址:
- packagist: https://packagist.org/packages/mouday/more-echo
- github: https://github.com/mouday/more-echo-php
使用发布的包
安装包
composer require mouday/more-echo
- 1
示例
<?php
require './vendor/autoload.php';
use Mouday\MoreEcho\EchoText;
EchoText::echoText('Hello World!');
// Hello World!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
问题
1、提示composer.json找不到
[RuntimeException]
No composer.json present in the current directory (./composer.json),
this may be the cause of the following exception.
- 1
- 2
- 3
echo '{}' > composer.json
- 1
2、包不存在
通常我们使用的是阿里云的镜像地址,刚提交的包,可能没有及时同步,可以临时设置一下包下载地址
$ composer config repo.packagist composer https://packagist.org/
- 1
3、版本号找不到
[InvalidArgumentException]
Could not find a version of package mouday/more-echo matching your minimum-stability (stable).
Require it with an explicit version constraint allowing its desired stability.
- 1
- 2
- 3
给项目打一个版本号的标签
git tag 1.0.0
git push --tag
- 1
- 2
- 3
参考
php composer Packagist 创建第一个自己的包
101- composer [packagist]包制作(入门篇)
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/125367066
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)