You have developed out a gorgeous PHP package deal that you’ve got put your coronary heart and soul into, and are happy with. Now it is time for distribution, and one key half is guaranteeing you present others the best potential path to obtain and make the most of your package deal, however how?
Fortunately, For this there may be Packagist a free de facto repository for all PHP package deal dependencies. Learn to get your PHP package deal listed on Packagist, permitting others to obtain and set up it with one command.
Construction Your Bundle Correctly
Earlier than distribution, your package deal must be structured correctly and should make the most of namespaces. Resolve on a root namespace on your package deal. This may be something you would like, however usually it is suggested to make use of AuthorPackageName.
For instance, the namespace JSmithBlogPoster would inform others the developer of the package deal goes by the identify Jsmith and that is their package deal named BlogPoster.
With root namespace determined, create a /src/ listing in your undertaking, and transfer all PHP information to it. Each file should have the correct declaration on the prime, so all PHP information inside the /src/ listing should begin:
The namespace declaration is relative to the listing construction. For instance, when you’ve got a file at /src/Photos/Uploader.php, on the prime of the file you’ll have:
Final, guarantee the category declaration of all PHP information is identical because the filename, because the above instance exhibits. The Uploader.php file has the category identify Uploader.
Register a Packagist Account
Should you do not have already got one, create a Packagist account, it is fast and free. Though not required, it is commonplace observe to register with the identical username as within the root namespace you determined upon within the above part. If that username isn’t accessible, then select one thing related.
You need not do something with the Packagist account simply but, however it’s helpful to have it prepared.
Create composer.json File
Open a textual content editor, and inside the root listing of your undertaking create a composer.json file, with the next contents used for example:
"description": "A wonderful weblog posting package deal...",
"sort": "package deal",
There are solely two vital facets of the above file to be aware of.
First is the identify aspect on the prime. This which should be two strings separated by a ahead slash, the primary being your Packagist username, and the second being the specified identify of the package deal. Utilizing the above instance, the Packagist username is jsmith, and the package deal identify is blogposter.
Second, on the backside inside the autload part you’ll discover the road:
Modify this line so the foundation namespace you beforehand determined upon factors to the /src/ listing. When your package deal is put in by another person, Composer will mechanically map your root namespace to the sub-directory your package deal was put in in.
Add Your Bundle to Github
You have to add your PHP package deal to a Github repository, as that is the principle integration technique Packagist makes use of. Should you aren’t already accustomed to creating and publishing to a Github repository, please take a look at Easy methods to Create Your First Repository on GitHub for a superb step-by-step information.
Guarantee to add your whole PHP package deal together with all information inside /src/ and the composer.json file. Should you want to merely add all the undertaking listing to GitHub, open a terminal inside your undertaking listing and run:
git add ./*
git commit -m "Preliminary commit"
git push -u origin grasp
You additionally should add a tag to the GitHub repository, as tags are how Packagist retains observe of releases and model numbers of your packages. To mark the present repository as v0.1 run the instructions:
git tag 0.1
git push --tags
Add a PHP Itemizing on Packagist
Every thing is now in place, so with a smile you possibly can go forward and submit your PHP package deal for immediate itemizing on Packagist. Go to the Submit Bundle web page, and enter the URL to your GitHub repository, which will probably be one thing like:
Hit the Verify submit button, and Packagist will obtain the composer.json file out of your repository, plus any accessible tags / releases and different data.
The subsequent web page will affirm you want to publish the package deal; as soon as confirmed it is possible for you to to view your new Packagist itemizing at a URL akin to:
Set up Your Bundle With Composer
Attempt it out, and set up your PHP package deal with Composer. Should you do not have already got Composer put in, you could set up it with the next command:
sudo curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/native/bin --filename=composer
Create a brand new listing, and inside it run the command:
composer require jsmith/blogposter
Composer will work its magic, and you’ll discover a brand new /vendor/ sub-directory will seem, which in the event you look within, you will notice your PHP package deal!
Give it a check, and cargo your PHP package deal. Utilizing the above instance, if there was a file a /src/Blogger.php within the package deal, a fast check script to load it could be:
// Load composer dependences
// Get blogger
$shopper = new Blogger();
echo "Acquired a " . $shopper::class . "
Go Ahead, and Share With the World
You have now discovered how simple it’s to construction your PHP package deal appropriately, and checklist it on Packagist permitting anybody on the planet to immediately obtain and embody your package deal inside their undertaking with one single command.
Congratulations, you have now opened your PHP package deal as much as the principle distribution channel inside the PHP eco-system. Go ahead, and share your creativity and laborious work with the world.
Getting ready to code your first program? You should definitely comply with these key software program improvement steps.
About The Creator