创建一个类输出hello word

1.创建helllo目录,创建子目录src,在src目录下创建hello.php文件
 目录结构如下:
                       |- hello
                         |- src
                            |- hello.php

hello.php内容如下,注意命名空间大小写:


<?php
namespace Hello;

class hello
{
    static public function index()
    {
        return 'hello word !';
    }
}

/**
 * hello.php
 *
 * ...
 *
 * 2017 Copyright (c) https://note.jsx6.com
 *
 * 修改历史
 * ----------------------------------------
 * 2017/10/18, 作者: 降省心(QQ:1348550820), 操作:创建
 **/


2.打开cmd命令,执行如下过程:

D:\PHPTutorial\PHPTutorial\WWW\Composer\hello> composer init

  Welcome to the Composer config generator

This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [降省新/hello]:  jiangshengxin/hello
Description []:  JiangShengXin CeShi
Author [jiangshengxin <
1348550820@qq.com>, n to skip]: n
Minimum Stability []:  dev
Package Type (e.g. library, project, metapackage, composer-plugin) []:  library
License []:  MIT

Define your dependencies.

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": "jiangshengxin/hello",
    "description": "JiangShengXin CeShi",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "jiangshengxin",
            "email": "
1348550820@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

Do you confirm generation [yes]? yes

这样,根目录下就生成了composer.json文件,
目录结构如下:
                       |-- hello
                          |-- src
                             |-- hello.php
                          |-- composer.json

 

3.编辑 composer.json 文件,加入黄色区域内容, 如下:

 {
    "name": "jiangshengxin/hello",
    "description": "JiangShengXin CeShi",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "jiangshengxin",
            "email": "1348550820@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4": {
            "Hello\\": "src/"
        }
    },
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "
https://packagist.phpcomposer.com"
        }
    }
}

4.测试类,是否可以正常工作,CMD命令如下:

 
D:\PHPTutorial\PHPTutorial\WWW\Composer\hello> composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

运行完成,
目录结构如下:
                       |-- hello
                          |-- src
                              |-- hello.php
                          |-- composer.json
                          |-- vendor
                             |-- composer
                                |--有好多,懒得写了...
                             |--autoload.php


5.在hello目录下,创建测试文件demo.php,进行测试


<?php
header("content-type:text/html;charset=utf-8");

require_once __DIR__.'/vendor/autoload.php';

use Hello\hello;
echo hello::index();

/**
 * demo.php
 *
 * ...
 *
 * 2017 Copyright (c) https://note.jsx6.com
 *
 * 修改历史
 * ----------------------------------------
 * 2017/10/18, 作者: 降省心(QQ:1348550820), 操作:创建
 **/


在项目根目录下执行命令  php demo.php
如果终端打印出"hello word !"那祝贺你测试通过!

6.将项目发布到Github。我们先去Github 创建一个公有仓库命名“hello”, 我们在项目根目录使用Git命令来完成发布。
在根目录(hello目录)里创建.gitignore文件,把vendor目录和composer.lock文件排除git在外。
.gitignore文件内容如下:

 
vendor/*
composer.lock

推送代码,
完成

7.发布到Packagist.org

    首先要在Packagist上注册账号并登录,官网:https://packagist.org/
    点击顶部导航条中的Summit按钮
    在输入框中输入github上的仓库地址,如:https://github.com/jiangshengxin/dfa.git
    然后点击Check按钮
    Packagist会去检测此仓库地址的代码是否符合Composer的Package包的要求
    检测正常的话,会出现Submit按钮,再点击一下Submit按钮,我们的包就提交到Packagist上了
    以后更新代码可以先从Packagist获取token然后去github 配置下对应的GitHub Service Hook实现代码提交后Packagist自动拉取更新
完成...

-----------------------------------------------------使用示例----------------------------------------------------

引用这个类包的时候,方法如下:
1.在项目目录下面,新建文件 composer.json 编辑内容如下:
 
{
    "require": {
        "jiangshengxin/hello":"dev-master"
    }
}

2.CMD执行下面命令,等待安装,成功则如下:
 
D:\PHPTutorial\PHPTutorial\WWW\Composer\test> composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing jiangshengxin/hello (dev-master bd14882): Cloning bd14882738 from cache
Writing lock file
Generating autoload files

完成~

 

 

---------------------------------------------------------------------------------------------
唯有志存高远,方能风行天下。

道之所存,虽千万人吾往矣! 情之所钟,虽千万里吾念矣~

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。