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

To control the priority of Composer multi-repository packages in PHP projects, simply arrange the order of repository as needed in composer.json. First, list the repositories in the repositories array by priority, and Composer will look for packages in this order; second, if you want a private repository to take priority over Packagist, put it at the top of the list; in addition, you can use the path repository to achieve local override, which is suitable for the development and testing stage; finally, be careful to avoid conflicts caused by packages of the same name in different repositories. It is recommended to use a unique naming prefix and use the composershow command to check the source.

Packagist is Composer's default package repository for centralized management and discovery of PHP packages. It stores the metadata of the package instead of the code itself, allowing developers to define dependencies through composer.json and get the code from the source (such as GitHub) at installation time. Its core functions include: 1. Provide centralized package browsing and search; 2. Manage versions to meet dependency constraints; 3. Automatic updates are achieved through webhooks. While custom repositories can be configured to use Composer, Packagist simplifies the distribution process of public packages. The publishing package needs to be submitted to Packagist and set up a webhook, so that others can install it with one click through composerrequire.

Managing environment configuration in PHP projects can be achieved in a variety of ways. First, use the .env file of the Dotenv library to create configuration files for different environments such as .env.development and .env.production, and load them through vlucas/phpdotenv, and submit the sample files and ignore the real files; second, store non-sensitive metadata in the extra part of composer.json, such as cache time and log levels for script reading; third, maintain independent configuration files such as config/development.php for different environments, and load the corresponding files according to the APP_ENV variable at runtime; finally, use CI/C

To quickly get detailed information about a specific package in Composer, use the composershowvendor/package command. For example, composershowmonolog/monolog, which will display version, description, dependencies and other information; if you are not sure of the name, you can use some names to combine --platform to view platform requirements; add --name-only to simplify output; use -v to display more detailed content; support wildcard search, such as monolog/*.

PHP's automatic loading methods include PSR-0, PSR-4, classmap and files. The core purpose is to implement automatic loading of classes without manually introducing files. 1. PSR-0 is an early standard, and automatically loads through class name and file path mapping, but because the naming specifications are strict and the support for underscores as directory separators have been rarely used; 2. PSR-4 is a modern standard, which adopts a more concise namespace and directory mapping method, allowing a namespace to correspond to multiple directories and does not support underscore separation, becoming the mainstream choice; 3. classmap generates a static mapping table of class names and paths by scanning the specified directory, which is suitable for legacy code that does not follow the PSR specification, but new files need to be regenerated and large directories

To use Composer to set up automatic loading of PHP projects, you must first edit the composer.json file and select the appropriate automatic loading method. If the most commonly used PSR-4 standard is adopted, the mapping of namespace and directory can be defined in the psr-4 field of autoload, such as mapping MyApp\ to src/directory, so that the MyApp\Controllers\HomeController class will automatically load from src/Controllers/HomeController.php; 1. After the configuration is completed, run composerdumpautoload to generate an automatic loading file; 2. If you need to be compatible with old code, you can use it.

Installing Composer takes only a few steps and is suitable for Windows, macOS, and Linux. Windows users should download Composer-Setup.exe and run it to ensure that PHP is installed or XAMPP is used; macOS users need to execute download, verification, and global installation commands through the terminal; Linux users operate similarly to macOS, and then use the corresponding package manager to install PHP and download and move the Composer file to the global directory.

ComposerpluginsextendComposer’sfunctionalitywithoutalteringitscore.Theyautomatetasks,enforcerules,orintegratewithtoolsbyhookingintoComposer'seventsandAPIs.Commonusesincluderunningcustomscripts,modifyingpackagebehavior,andintegratingchecks.Toinstall:1
