Use Composer Github Packagist to create and release a shared PHP package, githubpackagist

Source: Internet
Author: User
Tags autoload composer install git client

Use Composer Github Packagist to create and release a shared PHP package, githubpackagist

Reference Source:





1. download and install the Composer dependency management tool

2. Create a Github account and a code hosting platform

3. Create a Packagist account and package Management Platform

4. download and install the git Client


Release code to Github

1. Create a code repository


2. Clone code



[root@localhost composer]# git clone empty Git repository in /var/www/html/composer/repeat-test/.git/warning: You appear to have cloned an empty repository.

Here, I use the ssh key cloning method, provided that a key pair needs to be built on Mr, and then the public key is uploaded to Github.

Here is a warning: An empty repository is cloned.


3. Push code

[root@localhost repeat-test]# touch[root@localhost repeat-test]# vi [root@localhost repeat-test]# git add .[root@localhost repeat-test]# git commit -m 'add file'[root@localhost repeat-test]# git push origin master


Then, refresh the page after the repository is created, and the following content is displayed:


Here, the basic code hosting has been completed, but we need to use compser to manage project dependencies so proceed to step 4.


4. initialize the composer. json file and push it to the remote repository.

Execute the composer init initialization command

[Root @ localhost repeat-test] # composer initDo not run Composer as root/super user! See for details Welcome to the Composer config generator This command will guide you through creating your composer. json config. // Package name (<vendor>/<name>) [root/repeat-test]: sky/repeat-t // Description []: this is test // The Author has the email address Author [admin <>, n to skip]: sky <> // Minimum stable version of Minimum Stability []: // Package Type (e.g. library, proj Ect, metapackage, composer-plugin) []: library // License, License []: MIT // The following defines dependencies on Define your dependencies. wocould you like to define your dependencies (require) interactively [yes]? Nowocould you like to define your dev dependencies (require-dev) interactively [yes]? No {"name": "sky/repeat-t", "description": "this is test", "type": "library", "license": "MIT ", "authors": [{"name": "sky", "email": ""}], "require ": {}}// confirm to generate Do you confirm generation [yes]? Yes // do you want to add the vendor directory to. gitignorewocould you like the vendor directory added to your. gitignore [yes]? Yes


View the composer. json File

[root@localhost repeat-test]# cat composer.json {    "name": "sky/repeat-t",    "description": "this is test",    "type": "library",    "license": "MIT",    "authors": [        {            "name": "sky",            "email": ""        }    ],    "require": {}}


Add the autoload automatic loading mechanism and edit the composer. json file. The final result is:

{    "name": "sky/repeat-t",    "description": "this is test",    "type": "library",    "license": "MIT",    "authors": [        {            "name": "sky",            "email": ""        }    ],    "require": {},    "autoload": {        "psr-4" : {            "Sky\\Demo\\"  : "src"        },        "files" : [            "src/helper.php"        ],        "classmap"  : [            "src/Common"        ]    }}


Add the corresponding files according to the loading method configured by autoload. Click here for details about how to load the files.

Create the src directory under the repeat-test directory, and create the Hello. php file under the directory. The content is:

(Here only the file referenced in the psr-4 loading method is pasted)

<?phpnamespace Sky\Demo;class Hello{    private $name;    public function __construct($name = 'World')    {        $this->name = $name;    }    public function hello()    {        return 'Hello ' . $this->name;    }}


Finally, we need to update the automatic loading configuration to add our code to the automatic loading mechanism of composer for management.

Composer update

After running the command, you will find a local vendor directory. In this case, check that there is a composer folder in this directory. Check the content in the files autoload_psr4.php, autoload_files.php, and autoload_classmap.php. It will understand what has been done after the autoload configuration is updated.


Test whether our code is correct. Create the test. php file in the repeat-test directory with the following content:

require 'vendor/autoload.php';//hello world$hello = new Sky\Demo\Hello('worlddd );echo $hello->hello() . PHP_EOL;


Push to remote Repository

[root@localhost repeat-test]# git add .[root@localhost repeat-test]# git commit -m 'init composer.json file and auload'[root@localhost repeat-test]# git push origin master


So far, others can automatically Load Code into their projects through composer. However, because composer automatically loads packages on the Packagist platform, you need to specify the remote repository address as the address on Github, as shown in the following configuration after the composer. json file is adjusted:

{    "repositories":[        {            "type":"vcs",            "url":""        }    ],    "require": {        "sky/repeat-t":"dev-master"    }}


For more information about how to use composer, see here.

Note: If the preceding warehouse address does not contain the composer. json file, if you perform the following operations, the file cannot be found.


We recommend that you create a new directory for the composer. json file and the installation below. The following files will be generated after you execute composer install in/var/www/html/test:

Composer. lock vendor/

Here we load the code in the repeat-test repository to our project. You only need to use require vendor/autoload. php to use it.


Publish code to the Packagist Platform

After the copy address is verified successfully, submit it again and pay attention to a warning on the page, prompting you to configure the automatic update Hook between Github and Packagist. Click the GitHub Service Hook link as prompted in the Wizard, copy your own api token on Packagist and go to Github to find the corresponding repository. Click Settings-> Integrations & services, click "Add services" and select "Packagist" from the drop-down list. You need to enter the Packagist account name, api token, and the third domain can be written or not (write the Packagist domain name) click update service and then test service.


Note: The following prompt may be prompted for verification before release:

The vendor is already taken by someone else. You may ask them to add your package and give you maintainership access. The packages already in that vendor namespace can be found at sky


Probably, there is already a Sky Supplier name. You can ask them to add your package and grant you maintenance permissions. Let's change the name here, so we should pay attention to this situation when naming.

Now we can use composer install require sky/repeat-t dev-master to directly reference it in the project.


If you have any questions, please correct them. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.