Advertisement

How to Submit My Library to Packagist.org

 


Packagist.org is the default package repository for PHP libraries. It allows developers to easily discover and install libraries using Composer, a dependency management tool for PHP. If you have developed a PHP library and want to make it available for others to use through Packagist.org, this article will guide you through the submission process. We'll cover the necessary steps, including configuring your library, creating a composer.json file, and publishing your library to Packagist.org.

Step 1: Prepare Your Library

Before submitting your library to Packagist.org, ensure that it meets the following requirements:

- Your library should have a unique name that does not conflict with existing libraries on Packagist.org. Choose a name that accurately represents your library.

- The library should follow the PHP best practices and be well-documented.

- Ensure that your library is hosted on a version control system (e.g., Git).

Step 2: Create a composer.json File

A `composer.json` file is essential for defining your library's metadata and dependencies. Create a new file called `composer.json` in the root directory of your library and include the following content:


{
    "name": "your-username/your-library",
    "description": "A brief description of your library",
    "type": "library",
    "keywords": ["your", "keywords", "here"],
    "license": "MIT",
    "authors": [
        {
            "name": "Your Name",
            "email": "your@email.com"
        }
    ],
    "require": {
        "php": "^7.4",
        "other-dependency": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "YourNamespace\\": "src/"
        }
    }
}


Make sure to replace the placeholders with the appropriate values. The `autoload` section specifies the PSR-4 autoloading configuration for your library. Adjust the `require` section to list any dependencies required by your library.

Step 3: Publish Your Library

To publish your library to Packagist.org, follow these steps:

1. Commit and push your library to a version control system repository (e.g., GitHub).

2. Visit (https://packagist.org) and log in using your GitHub or Bitbucket account.

3. Click on "Submit" in the navigation bar and enter the URL of your library's repository.

4. Packagist.org will fetch the composer.json file from your repository. Verify that the details are correct.

5. Once you are satisfied with the details, click on "Submit" to publish your library to Packagist.org.

6. Packagist.org will automatically detect new releases of your library based on Git tags. Ensure you create tags following semantic versioning (e.g., `v1.0.0`).

Congratulations! Your library is now available on Packagist.org, and developers can install it using Composer.

Example Code

As an example, let's assume you have developed a PHP library called "MyAwesomeLibrary" that provides useful utility functions. You've hosted the library on GitHub under your username "myusername." Here's how your composer.json file would look:

{
    "name": "myusername/myawesomelibrary",
    "description": "A powerful PHP utility library for everyday use",
    "type": "library",
    "keywords": ["php", "utility", "library"],
    "license": "MIT",
    "authors": [
        {
            "name": "Your Name",
            "email": "your@email.com"
        }
    ],
    "require": {
        "php": "^7.4"
    },
    "autoload": {
        "psr-4": {
            "MyAwesomeLibrary\\": "src/"
        }
    }
}


Remember to adjust the placeholders with your actual information.

Example I have : https://packagist.org/packages/derilkillms/

Conclusion

By following the steps outlined in this article, you can easily submit your PHP library to Packagist.org and make it available to the wider PHP community. Ensure that your library adheres to best practices, and don't forget to maintain and update it regularly. Publishing your library on Packagist.org increases its discoverability and allows others to easily include it in their projects using Composer, fostering collaboration and code reuse within the PHP ecosystem.

Post a Comment

0 Comments