Close ×



Get A Free Quote

Let’s get to work! Contact us with the form below at no obligation to you. Together, we’ll set the project’s scope and demonstrate you value that the experts at cmsMinds bring to projects like yours.

Tell Us A Bit About Yourself

    Select As Many Of These As Apply

    How To Create Your Own Composer/Packagist Package

    Nov 2018 | by Ravi

    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.

    How to create your own


    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.

    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 file content should like

    namespace HelloWorld;
    class SayHelloWorld
    public static function world()
    return 'Hello World, Composer!';

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

    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”

    It requires dependency minimum version 5.3 and tells composer to autoload all files “HelloWorld” namespace inside “src” dir.

    First, we need to make sure our composer file is working properly. For that Open terminal & go to your project directory to run a 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

    require_once __DIR__ . ‘/../vendor/autoload.php’;

    use HelloWorld\SayHello;

    echo SayHello::world();

    Once the 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:

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

    Ravi SR. PHP DEVELOPER - cmsMinds
    Nov 2018 Ravi


    Recent Post