Steps to create your first PHP Composer package

In my previous article “Composer — a PHP dependency manager”, I told you how to install and use PHP Composer on Ubuntu. In this article I’ll tell you how to create a package using composer and submit it to packagist. As a recap, packagist is the repository of packages and we can search/ install/update them via composer. Packagist also encourages user to submit their own packages to strengthen the community. Now let’s begin with creating a testpackage and submit it to packagist.

# Prerequisite

To submit any package on packagist there are some prerequisites:

Repository (Here, I am using GitHub)

Composer

# Creating a Repository

Creating a repository is very easy. Here, I created a GitHub repository named testpackage and cloned it on my local system.

$ sudo git clone https://github.com/digvijaykrtiwari/testpackage.git

You can create your own repository on GitHub and clone it to your local system. Create a file say “testpackage.php” and then, create a class

You can create multiple file and directory structure to place your code.

# Creating composer.json

To submit a package on packagist composer.json is required, which has information about package, vendor, version, stability etc. Basically, when our package/plugin is ready to deploy, we are installing composer into it. So that, after submitting the package to packagist composer can call it. We don’t need to do it manually but composer will do it.

# Commit and push the file

Once composer.json file has been created, you are ready to deploy your plugin. Additionally, you can add PHP version or other dependencies into composer.json manually. Now it is required to commit and push your project to repository.

# Submit Package to packagist

Now, login packagist with your credentials and click on submit on top navigation and submit your repository URL.

Verify your package and submit. After successful submission, packagist will provide composer installable information. That you can use to install .

Additionally, You can use your packagist API token to handshake your GitHub and packagist so that if you update your package any time, your package will get updated on packagist

Try this and leave comment if you face any issue. See you soon with a new topic.