Let’s create something better together.

If you prefer phones, we have one of those too: +1 978 455 4515










    • Project Info
      icon
    • Technology
      icon
    • Contact Details
      icon







      BackNext








      Back

      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.

      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

      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”
      },
      “autoload”:{
      “psr-0”:{
      “HelloWorld”:”src/”
      }
      }
      }

      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
      <?php

      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

      Author's Bio

      Jayesh Makwana
      Jayesh Makwana

      Jayesh Makwana writes with one goal in mind: to make Drupal easy for everyone. From his first steps with Drupal 6 to mastering the latest updates, he’s a true Drupal geek, passionate about sharing his knowledge. Whether it’s tips on Drupal migration, upgrading your site, or catching up on the newest features, Jayesh’s articles are your friendly guide. He simplifies complex topics, making them accessible to Drupal users of all levels. Follow Jayesh for insights that enlighten and inspire, all delivered with the enthusiasm of someone who loves what they do.

      Share This Article:

      Recent Blog
      VIEW ALL BLOGS