What are Composer events (e.g., post-install-cmd, post-update-cmd)?
Jul 13, 2025 am 01:19 AMComposer events are hooks triggered at a specific stage of Composer workflow. They are used to run custom scripts or commands. Common events include post-install-cmd and post-update-cmd, which are executed after composer install and composer update. Others include pre-install-cmd, pre-update-cmd, post-autoload-dump, etc. The corresponding operations can be defined in the scripts part of composer.json, such as executing shell commands or calling PHP classes. Pay attention to script order, compatibility and exit code when using them. Post-install-cmd is suitable for new installation scenarios, and post-update-cmd is suitable for synchronization operations after updating dependencies. It is also recommended to keep script idempotence, avoid running for a long time, and can be skipped with --no-scripts.
Composer events are hooks that let you run custom scripts or commands at specific points during the Composer workflow. They're super handy for automating tasks like clearing caches, generating autoload files, or running setup commands after dependencies are installed or updated.
Common Composer Events You'll Use
There are several built-in events in Composer that trigger at different stages of package management. Two of the most commonly used ones are:
-
post-install-cmd
– runs aftercomposer install
-
post-update-cmd
– runs aftercomposer update
These are especially useful when you want to make sure certain actions happen automatically after your dependencies change.
Other common events include:
-
pre-install-cmd
/pre-update-cmd
-
post-autoload-dump
-
post-package-install
/post-package-update
You can define what happens during these events in your composer.json
.
How to Define Scripts in composer.json
To use an event, you map it to a script inside the "scripts"
section of your composer.json
. Here's a basic example:
{ "scripts": { "post-install-cmd": [ "php artisan config:clear", "echo 'Installation complete!'" ], "post-update-cmd": "php artisan migrate" } }
Each event can run one or more commands. You can also call PHP classes that implement the Composer\Script\CommandEvent
interface if you need more control.
A few things to note:
- Commands are run in the order they appear
- Shell commands should be compatible with the system where Composer is running
- If a script fails (returns non-zero exit code), Composer will stop execution by default
This makes it easy to integrate environment-specific steps without having to remember to run them manually every time.
When to Use post-install-cmd vs post-update-cmd
While both events run after Composer operations, they serve slightly different purposes:
- Use
post-install-cmd
when you want something to happen only when packages are freshly installed — for example, on a new deployment or CI build. - Use
post-update-cmd
when you want actions triggered even when updating existing packages — useful for running database migrations or cache rebuilds after changes.
In practice:
- On a fresh server, both events might run if you use
composer install
andcomposer update
as part of setup - In local development, you might run
composer update
often, sopost-update-cmd
ensures your app stays in sync
Choosing the right one helps avoid unnecessary steps or missed updates depending on your workflow.
A Few Gotchas and Tips
Here are some practical tips when working with Composer events:
- Make sure your scripts are idealpotent — meaning they can safely be run multiple times
- Avoid long-running processes unless necessary; Composer waits for each script to finish
- For complex logic, write a small PHP class and register it via
"MyClass::myMethod"
- You can disable script execution with
--no-scripts
if you ever need to skip them temporarily
Also, keep in mind that scripts run in the context of the current working directory where Composer was executed, so path handling matters.
Basically that's it.
The above is the detailed content of What are Composer events (e.g., post-install-cmd, post-update-cmd)?. 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 using Composer in a production environment, you need to pay attention to safety, stability and performance. 1. Use composerinstall-no-dev to reduce unnecessary development dependencies and reduce online environment risks; 2. Always submit and rely on composer.lock files to ensure version consistency, and avoid using updates during deployment; 3. Optional configuration platform-check=false ignores platform differences warnings, which is suitable for building packaging scenarios; 4. Enable APCU to accelerate automatic loading to improve performance, especially suitable for high concurrency services, while paying attention to namespace uniqueness to avoid cache conflicts.

To check whether Composer is installed correctly, first run the composer--version command to view the version information. If the version number is displayed, it means that it is installed. Secondly, use the composerdiagnose command to detect configuration problems and ensure that the environment variables and permissions are normal. Finally, try to verify the functional integrity through the composerrequiremonolog/monolog installation package. If the vendor directory is successfully created and the dependency is downloaded, it means that Composer is fully available. If the above steps fail, you may need to check whether PHP has been installed globally or adjusted system path settings.

To install the Composer plug-in, please first confirm that Composer is installed and the composer.json file exists, and then follow the following steps: 1. Make sure that Composer has been installed and created composer.json; 2. Search and copy the required plug-in name on Packagist; 3. Use the composerrequirequire command to install the plug-in, such as composerrequiredealerdirect/phpcodesniffer-composer-installer; 4. Verify whether the plug-in is effective and check compatibility and configuration. Follow these steps to correctly install the Composer plug-in.

You can make Composer use a specific PHP version by specifying PHP binary files, using version management tools, or configuring composer.json. 1. In Unix-like systems, you can set an alias or export the COMPOSER_PHP environment variable to directly specify the PHP binary file; 2. Use tools such as phpenv or brew to switch the PHP version to achieve global or project-level version control; 3. Configure the platform.php field in composer.json to declare the PHP version required for the project to ensure that the dependency check is correct; 4. Windows users can call the specified PHP version by modifying the PATH environment variable or creating a batch script to call the specified PHP version.

To add a custom repository to the Composer configuration, edit the composer.json file in the project and specify the repository information under the "repositories" key. The specific steps are as follows: 1. Determine the repository type, such as VCS (Git, SVN, etc.), Composer, PEAR or Package; 2. Add the "repositories" block in composer.json and fill in the repository type and URL. For example, when using a VCS-type Git repository, the format is {"type":"vcs","url":"https

ToupdateyourpackageonPackagist,firstensureyourcomposer.jsonisupdatedwiththecorrectversion,dependencies,andmetadata,thencommitandpushchangestoyourrepository.1.Updatecomposer.jsonwithnecessarychangessuchasversion,dependencies,ormetadataandcommitit.2.Ta

When you encounter the "Yourplatformdoesnotatsatisfythatrequirement" error, you can use the --ignore-platform-reqs parameter to ignore the platform requirements for installation. The full name of this parameter is --ignore-platform-requirements. It is used to skip the PHP version, extension and other checks specified in composer.json when executing composerinstall or update. For example, if the current PHP version is 8.0 but the configuration requires 8.1, an error will be reported by default. If you add this parameter, the check will be skipped. Applicable scenarios include: 1. Local environment and true in containerized deployment or CI environment

To add dependencies to composer.json, the most common method is to use the composerrequire command, followed by manually editing the composer.json file. 1. Use composerrequiredor/package to automatically add the latest stable version dependencies and install them; 2. You can specify the version such as composerrequiredor/package: 1.2.3 or use the constraint character such as ^2.0; 3. This command will synchronize the update of composer.json and composer.lock and automatically handle the dependencies; 4. Manually edit suitable for batch addition or template projects, you need to maintain the version yourself and run c
