Create package for PHP with composer

  1 year ago     Views:  621  
Site Admin
4 Follows


PHP has become more popular programing language in website development. Composer is the one of the major key factor. Sometimes you need some functionality work that you already worked one project. Or when you search in the web, most of the time, you will already find that in github repository. In that case, you just have to download that library with git and put into your project, sometimes need to add your configurations and you can use that library in your poroject with composer.

Composer is the PHP package manager by which you can use packages developed by community. You can also create your own package and share it with the other developers. In this tutorial article, we will create simple package and use it. I will create simple project which will provide IP address of the user.

composer.json file

To create package, first you need to initialize your package with composer.json file. composer.json is the file where your packge information is stored. To create composer.json file, run the bellow command:

composer init

When you run this command, it will ask for package information, like package name, description, author, type, licence etc.. It will also ask if your package requires other packages. After that, you can see bellow composer.json file. Here is the file that I have create.

{
    "name": "jiteshmeniya/ipgetter",
    "description": "Get IP address of the user",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "jiteshmeniya99",
            "email": "jiteshmeniya99@gmail.com"
        }
    ],
    "require": {}
}

Other than composer.json, there are also some files in the root of package. README.md file is the basic documentation that will describe about package and how to use it. LICENSE file is terms how other developers are allowed to use and modify package code. src is the main directory where all of your code are reside

Now let's talk about code. Create file in the src directory. Remember your file name should be match with class defined in the file. You should create one class in one file. If you need another class, create a new file. Also you can not create multiple class with same name. It is recommend you use new classname for every file.

I have created bellow class in src/getip/IPGetter.php file.

<?php

namespace IPGetter;

class IPGetter
{
    /**
     * get IP address of the user
     *
     * @return void
     */
    public static function getIPAddress()
    {
        return $_SERVER['REMOTE_ADDR'];
    }
}

Now we have to modify composer.json file as we need to autoload all of src classes.

{
    "name": "jiteshmeniya/ipgetter",
    "description": "Get IP address of the user",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "jiteshmeniya99",
            "email": "jiteshmeniya99@gmail.com"
        }
    ],
    "require": {
        "php": ">=5.6"
    },
    "autoload": {
        "psr-4": {
            "IPGetter\\": "src/getip"
        }
    }
}

In the require, we have added minimum php version reuired. So if your package has code that can only works in specific PHP version, you can specify here. So after that someone will try to install this will get error without install. Also in the autoload, we have added the class namespace, so all the files inside src/getip directory will be loaded namespace "IPGetter".

Send package to Packagist.

After package has been created, you can share it other developers with github and send it to packagist. By doing this, other developers can use your package with just one command. To upload your package to github, check this article[How to upload project to git?]

Now go to Packagist and submit your Github package link.

Now you will get command for install package. In my case I get command like this:

composer require jiteshmeniya/ipgetter

Use package in your Project

Whenever you need to use package in your project, run that command in project. It will create vendor directory with with your package folder along with composer folder. composer folder loads all of vendor folders classes. There is also autoload.php file in vendor folder which includes composer folders all class. So all you have to do is include vendor/autoload.php file in your index.php file. I have created bellow index.php file and used package class.

<?php

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

use IPGetter\IPGetter;

$ip_address = IPGetter::getIPAddress();

echo($ip_address);exit;

Conclusion

In this way, you can also create dynamic and complex packages according to your requirement and use in your all projects. This way it also helps other developers to use your package and contribute to community.

If you have any query or suggestion, please comment bellow.


Was this article helpful?

2 out of 2 person found this article helpful.

Leave a comment

OR

No Comments