使用composer编写自己的库(composer dev)

注:需要在windows上安装composer、git等软件

1、composer使用

详见:
https://docs.phpcomposer.com/

2、在github创建仓库、发布,最后在packagist上提交包

2.1、登录https://github.com,点击New repository创建仓库

2.2、创建仓库之后,我们可以使用git工具下载仓库代码

git clone https://github.com/xxx/lawademo.git

2.3、使用composer init进行初始化

文件结构及composer.json内容,如下图所示:

不想命名空间为Lawademo\\Demo,有点长,对composer.json文件做如下修改:

{
    "name": "lawademo/demo",
    "license": "Apache-2.0",
    "autoload": {
        "psr-4": {
            "lawademo\\": "src/"
        }
    },
    "require": {
        "php": ">=5.3.0"
    }
}

2.4、在src目录下编写库文件

在src/test目录下编写Hello.php文件,内容如图所示:

接下来编写index.php测试文件并测试Hello.php文件功能是否正常

在执行index.php文件之前,先要执行composer update命令,然后执行index.php脚本,查看结果是否输出hello world

2.5、将代码提交到github仓库

注意:代码仅包含src目录和composer.json文件,vendor目录不要上传

# 添加要提交文件
git add .
 
# 填写描述/说明
git comment -m "说明"
 
# 提交到本地仓
git commit
 
# 推送到github仓库
git push
 
# 或者使用下面命令推送到github仓库
git push -u origin main

2.6、在github.com发布版本

2.7、在https://packagist.org/packages/submit上填写仓库地址

2.8、使用composer require xxx/lawademo,并编写index.php文件进行测试

注:其中index.php的相关操作同第4步的操作

3、可能出现的问题

Q:unable to access 'https://github.com/xxx/yyy.git/': OpenSSL SSL_read: Connection was reset, errno 10054

A:执行下面命令git config --global http.sslVerify false