Recently, I recently tried my best to upload a lot of code to open source repositories like Github and Codepen . It makes starting up new projects so much simpler when I can copy a summary from my Github or copy a piece of CSS / JS from CodePen . But what happens when I need to include libraries or modules in some projects? Cloning and copying paste just doesn’t pack it at that time.
Packed into NPM and Composer (or Packagist ). NPM is package management software for Javascript using Node , and Composer is similar but used in PHP .
Today, we are considering submiting a PHP “package” to Composer through Packagist, so we can install the package into the project using composer require
Simple steps: Make it easy with 4 steps (except for the part you code package in PHP ). Before you begin, you need to create a repository on Githib and commit the code on it. Also need an account on Github and Packagist .
- Create composer.json file in the project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | { "name": "your-brand-name/your-project", "type": "library", "description": "Your package description goes here", "keywords": ["relevant","tags","go","here"], "homepage": "https://yourcompany.com", "license": "MIT", "authors": [ { "name": "Nhan Nguyen", "email": " <a class="__cf_email__" href="/cdn-cgi/l/email-protection" data-cfemail="8fe1e8faf6eae1e1e7eee1bfb6ece1fbfbcfe8e2eee6e3a1ece0e2">[email protected]</a> ", "homepage": "http://nhannvt.com", "role": "Developer" } ], } |
- Create a new repo on Github and push your local code to the remote Github repo.
- Submit your package to Packagist
- Finish! Your package is online and Packagist will provide Composer with the name of your project. (eg composer require username / package-name) You will be able to view your package directly at: https://packagist.org/packages/your-username/package-name
It’s easy, no reason One of the biggest programming principles I try to apply is DRY, don’t repeat yourself. Once your code may already be hosted on Github, it’s just a two-step process to get your code where its version is controlled. Instead of relying on git clone or subodules, using a composer dependency would be much more efficient.
Get my code to test it
1 2 | composer require nhannguyen09cntt/php-discourse-sdk |
Reference: https://github.com/nhannguyen09cntt/php-discourse-sdk
Source: https://4rum.vn/t/h-ng-dnto-php-package-cho-composer/3077