


How to create and publish PHP packages using Composer: A practical case
Apr 18, 2025 am 11:15 AMYou can learn composer through the following address: Learn address
Article content
During the development process, we often need to package the code we write into libraries so that other developers or projects can easily use it. Recently, I encountered many challenges and learning opportunities while trying to publish a simple PHP package to Packagist. In this article, I will share the process of how to create and publish a PHP package called "do365/142-composer-hello-world" using Composer, as well as the experience and gains from it.
Challenges encountered
Before starting this project, I was not very familiar with the use of Composer and Packagist. Here are some of the main challenges I encountered:
- Package Initialization : How to correctly initialize a Composer package and set its dependencies.
- Publishing Process : How to submit a package to Packagist and make sure it is published correctly.
- Automated publishing : How to set up an automatic publishing process so that packages on Packagist can be automatically updated every time a code is pushed.
Use Composer to solve the problem
To solve these challenges, I followed the steps below:
1. Initialize the package
First, I use Composer's init
command to initialize my package:
<code class="bash">composer init --name "do365/142-composer-hello-world" --require "monolog/monolog:1.24.0" -n</code>
This command creates a new composer.json
file and specifies the name and dependencies of the package (in this example, I chose monolog/monolog
as the dependency).
2. Release package
Next, I logged in to Packagist.org and submitted my Git repository address:
- Log in https://packagist.org
- Submit
git@gitlab.com:do365-public/142-composer-hello-world.git
After submission, Packagist automatically detects and imports my package.
3. Release check
To make sure my package has been published successfully, I visited the following link to check:
- https://packagist.org/packages/do365/142-composer-hello-world
Here, I can see that my package has been successfully launched and can be used by other developers.
4. Automatically publish settings
In order to automatically update packages on Packagist every time I push the code, I set up an automatic publishing process in GitLab:
- In GitLab, go to the settings of my project
do365-public/142-composer-hello-world
. - Select
Integrations
and thenPackagist
. - Enter my Packagist username and token.
In this way, every time I push the code to GitLab, Packagist will automatically update my package, greatly simplifying the release process.
The advantages and practical application effects of Composer
Through this process, I deeply realized the power and convenience of Composer:
- Dependency management : Composer makes managing the dependencies of packages extremely simple. I just need to declare the dependencies in
composer.json
, and Composer will automatically handle them. - Version control : With Composer, I can easily manage different versions of the package, ensuring that other developers can choose the right version.
- Automation : The integration of Composer and Packagist makes release and update packages automated, greatly improving development efficiency.
Overall, the process of creating and publishing PHP packages using Composer not only solved my actual problem, but also gave me a deeper understanding of dependency management and version control. If you also need to publish your own PHP package, I highly recommend using Composer, which will bring great convenience and efficiency to your development work.
The above is the detailed content of How to create and publish PHP packages using Composer: A practical case. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When you open PyCharm for the first time, you should first create a new project and select a virtual environment, and then be familiar with the editor area, toolbar, navigation bar, and status bar. Set up Darcula themes and Consolas fonts, use smart tips and debugging tools to get more efficient, and learn Git integration.

To develop a complete Python Web application, follow these steps: 1. Choose the appropriate framework, such as Django or Flask. 2. Integrate databases and use ORMs such as SQLAlchemy. 3. Design the front-end and use Vue or React. 4. Perform the test, use pytest or unittest. 5. Deploy applications, use Docker and platforms such as Heroku or AWS. Through these steps, powerful and efficient web applications can be built.

Verifying an IMEISV string in PHP requires the following steps: 1. Verify the 16-bit numeric format using regular expressions. 2. Verify the validity of the IMEI part through the Luhn algorithm. 3. Check the validity of the software version number. The complete verification process includes format verification, Luhn checking and software version number checking to ensure the validity of IMEISV.

There are three ways to install the NumPy library: 1. Use pip to install: pipinstallnumpy, which is simple but may encounter permissions or network problems; 2. Use conda to install: condainstallnumpy, which is suitable for Anaconda environment, and automatically resolves dependencies; 3. Install: gitclone from source code and compile, which is suitable for special needs but complicated processes.

Deploying and tuning Jenkins on Debian is a process involving multiple steps, including installation, configuration, plug-in management, and performance optimization. Here is a detailed guide to help you achieve efficient Jenkins deployment. Installing Jenkins First, make sure your system has a Java environment installed. Jenkins requires a Java runtime environment (JRE) to run properly. sudoaptupdatesudoaptininstallopenjdk-11-jdk Verify that Java installation is successful: java-version Next, add J

Create and manage multiple project workspaces in VSCode through the following steps: 1. Click the "Manage" button in the lower left corner, select "New Workspace", and decide the save location. 2. Give the workspace a meaningful name, such as "WebDev" or "Backend". 3. Switch the project in Explorer. 4. Use the .code-workspace file to configure multiple projects and settings. 5. Pay attention to version control and dependency management to ensure that each project has .gitignore and package.json files. 6. Clean useless files regularly and consider using remote development skills

Using VSCode in a multi-screen environment can solve layout and display problems by adjusting the window size and position, setting workspaces, adjusting interface scaling, rationally laying tool windows, updating software and extensions, optimizing performance, and saving layout configuration, thereby improving development efficiency.

The steps to create a package in Laravel include: 1) Understanding the advantages of packages, such as modularity and reuse; 2) following Laravel naming and structural specifications; 3) creating a service provider using artisan command; 4) publishing configuration files correctly; 5) managing version control and publishing to Packagist; 6) performing rigorous testing; 7) writing detailed documentation; 8) ensuring compatibility with different Laravel versions.
