Create your first ever PHP Packagist for Composer part 2

Andino Inyang
Backend Developers
Published in
3 min readSep 30, 2019
picture credit: https://whoisryosuke.com

Composer is a powerful tool used by PHP to manage dependencies, while Packagist allows the composer to find codes where they are located.

This is the second guide in our PHP packages series. You can find the previous parts here:

Series 1.

In the previous guide, we looked at how to create a composer using the file system. In this guide, we will be creating a composer via the terminal.

Structure your project

For the sake of this tutorial, we will be creating a composer package from scratch. Nevertheless, the composer can be added to an existing project. Let us take our example project name to be “hello-composer”. We will also take to note that the composer uses the “vendor/package” format internally. I would set vendor to inyang and package to hello-composer.

It’s best practice to store your files in “src” folder. The project structure should start like this: “hello-composer/src/HelloComposer/ComposerHello.php”.

<?php namespace HelloComposer;class ComposerHello{  public static function hello(){     return “Hello! Hope you are enjoying learning Composer and     packagist”;  }}

Initialize composer

Open your project root in terminal window and run the composer init command.

running composer init on terminal window

Now a composer.json file would be created in project root with the following contents:

{
"name": "inyang/hello-october",
"description": "PHP Packagist for composer example",
"authors": [
{
"name": "Andino Inyang",
"email": "doninyangm@gmail.com"
}
],
"minimum-stability": "dev", "prefer-stable" : true,
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"HelloComposer": "src/"
}
}
}

Update if otherwise. “composer install” inside your root dir:

composer install

Packing your Packagist

Go to Github and create a new public repo called “hellocomposer”, start the Git project inside your root dir and push it. Copy your git location you would need it, it would look like this:

git@github.com:username/hello-composer.git

Submit your packagist

Go to packagist.org and create a new package. Packagist requires a github source, you can use the force update features, but I advice you setup a hook so packages automatic syncs with your github repo. This can be done navigating the Settings>Service Hooks>Packagist service

Hurray you have just created your first packagist.

Conclusion

This the end of this series, but follow to get more interesting updates.

Happy coding! May the code always be in your favour as you leave your foot print in the Eco-system.

Thank you for reading my article and my editor Solomon Eseme. Here at my blog or medium I regularly write about backend development, digital marketing and content management system. To read my future posts simply join my publication or click ‘Follow’ Also feel free to connect with me via Twitter, Facebook, Instagram.

If you are interested in backend development (or you’re internet enthusiast) both (Mobile | Web | Desktop) videos subscribe to my Youtube channel, we will be posting a collection of help full tutorials and guides like this one for artisans.

If you enjoy this post, make sure to let us know and share it with your friends and subscribe to my growing channel.

Sharing is caring.

--

--

Andino Inyang
Backend Developers

Experienced Product Analyst skilled in market research, experiment management, strategy development, and cross-functional collaboration.