packagist

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

Git Hub

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

<?php
namespace testPackage;
 
class testPackage {
    public function pack() {
            return "Hello Packagist";
    }
}
?>

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.

$ composer initComposer init

Above command will create composer.json file into your plugin/package root directory.

# 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.

Commit

# Submit Package to packagist

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

Packagist

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.

Cheers!!!

 

  • I just added your RSS Feed on my RSS reader, it is so nice to read your blog.~,”,’

  • I got what you intend, regards for putting up. Woh I am delighted to find this website through google.

  • I am not very excellent with English but I find this really easygoing to interpret .

  • Thanks so much for this great article; this is the kind of thing that keeps me going through the day. Ive been looking around for your article after I heard about them from a friend and was thrilled when I was able to find it after searching for some time. Being a avid blogger, Im happy to see others taking initiative and contributing to the community. I just wanted to comment to show my appreciation for your post as its very encouraging, and many writers do not get the credit they deserve. I am sure Ill be back and will send some of my friends.