GET A FREE QUOTE

How to create your own Composer/Packagist package

Hello Friends, Today I will explain you how to contribute in PHP communities to create packages using Composer and Packagist. If you are a PHP developer & not used composer yet, then I strongly recommend to have a look on our previous post on Why you should use Composer and how to start using it to get better idea.

Composer:

Composer is a package manager for PHP. You can use packages built by the communities, and you can also contribute with your packages too. Here I’ll explain you how to create project/package, installation of composer and send it to packagist, from where other developers can use it for their projects.

How to Create the Package?

You can create new project or update composer in your existing project. Here I will create folder name “helloworld” with simple hello world class. For eg, If you required any complex class you can create and share with another developer.

Before starting the process, first we will go through composer’s folder structure. Composer mainly works under “vendor” folder & it looks like “vendor/foldername”. You can easily find package code under vendor folder. In Composer, required basic folder structure is side package src, tests.

Lets start step 1

Here I start with the project folder name “helloworld”. Inside the project folder create “src” folder. I highly recommend create src folder because its easy to manage your code. Inside src folder, create another folder “HelloWorld”, and inside helloworld create class file like below: –

“sayhelloworld.php”.
Sayhelloworld.php file content should like
<?php
namespace HelloWorld;
class SayHelloWorld
{
    public static function world()
    {
        return 'Hello World, Composer!';
    }
}

Once you finished with above package steps, start below mention steps.

Now start with Composer

Start with Installation of composer in your project. After installation create composer.json file inside in your project root directory. Use terminal go to your project directory and follow bellow step.

After completion of above steps, check your project folder for composer.json file, it should be created into your folder. But before we use composer files, we need to do required changes.

{
    "name": "sagar/helloworld",
    "description": "This my first composer package",
    "type": "library",
    "authors": [
        {
            "name": "sagar patel",
            "email": "patel.sagar1508@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {
	“php”: “>=5.3.0”
     },
 “autoload”:{
	“psr-0”:{
		“HelloWorld”:”src/”
	}
 }
}

It require dependency minimum version 5.3 and tell composer to autoload all files “HelloWorld” name space inside “src” dir.

Testing Package

First, we need to make sure our composer file is working properly. For that Open terminal & go to your project directory to run command “composer Install”. Once you are done with composer installation, it (composer) creates tests folder in your root project directory. Now create test file inside tests folder.

test.php file content should be

<?php

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

use HelloWorld\SayHello;


echo SayHello::world();

Once file is created, check whether it’s working or not. Go to your terminal and run command php tests/test.php

You will get below output :

“Hello World, Composer!”. It’s working now.

You can create your own packages with complex class. For this you should have knowledge of OOPS concept.

About Author:

Sagar Rupareliya is a web developer by profession, handling PHP Development team at cmsminds with 5 years of experience. She is a Self-learner, strong command on logic implementation, best practice with many frameworks, Third party integration, API development.

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Quick Inquiry