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 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
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.
"description": "This my first composer package",
"name": "sagar patel",
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’;
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.
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