What are Composer scripts?
Composer scripts automate routine tasks during Composer's lifecycle by executing PHP functions or shell commands in response to specific events. 1. Define scripts in the "scripts" section of composer.json to run commands automatically. 2. Common events include pre- and post-install-cmd, pre- and post-update-cmd, post-autoload-dump, and post-create-project-cmd. 3. Scripts can be shell commands (e.g., php artisan optimize), static PHP methods (e.g., MyClass::myMethod), or closures, with PHP classes needing to be autoloadable. 4. Use arrays to chain multiple commands and ensure scripts are fast, non-interactive, and appropriate for the environment. 5. Be cautious with scripts from third-party dependencies to avoid unintended side effects. Composer scripts enhance workflow consistency and reduce manual errors when used effectively.
Composer scripts are custom PHP functions or shell commands that you can define in your composer.json
file to be automatically executed when certain Composer events occur—like installing, updating, or dumping autoloaders.

They’re a way to hook into Composer’s lifecycle and automate common development or deployment tasks, such as clearing caches, running tests, generating code, or sending notifications.
Why Use Composer Scripts?
Instead of manually running the same commands every time you install dependencies, you can automate them. For example:

- Run a PHP script after dependencies are installed.
- Execute a build step when autoloading is regenerated.
- Notify a service when a package is updated.
This helps keep your development workflow consistent and reduces repetitive tasks.
Common Composer Script Events
Composer provides a list of built-in events. You can attach your scripts to these:

-
pre-install-cmd
– Beforeinstall
runs -
post-install-cmd
– Afterinstall
completes -
pre-update-cmd
– Beforeupdate
runs -
post-update-cmd
– Afterupdate
completes -
post-autoload-dump
– After autoloader is regenerated -
post-create-project-cmd
– Aftercreate-project
finishes (useful for scaffolding)
How to Define Scripts
You add a scripts
section in your composer.json
:
{ "scripts": { "post-install-cmd": [ "php artisan clear-compiled", "php artisan optimize" ], "post-update-cmd": "php artisan migrate", "post-autoload-dump": [ "My\\Script::run" ] } }
In this example:
- After
composer install
, it clears and optimizes Laravel’s compiled files. - After
composer update
, it runs a database migration. - After the autoloader is rebuilt, it calls a static PHP method.
Types of Scripts
You can run:
-
Shell commands (e.g.,
php artisan optimize
) -
PHP static methods (e.g.,
MyClass::myMethod
) - Closure functions (in advanced cases using custom script handlers)
For PHP methods, the class must be autoloadable, and the method must be static.
Tips and Best Practices
- Keep scripts simple and fast—especially those running on every install.
- Avoid long-running or interactive scripts unless necessary.
- Use scripts for dev-only tasks? Put them in
require-dev
or use environment checks. - You can chain multiple commands using arrays.
- Scripts in dependencies can run too—be cautious with third-party packages.
Basically, Composer scripts help automate routine tasks tied to your dependency management. They’re not magic, but they save time and reduce human error when used wisely.
The above is the detailed content of What are Composer scripts?. 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)

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.

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

Youshouldusecomposerrequire--devroave/security-advisories:dev-mastertocheckforknownsecurityvulnerabilitiesduringdevelopment.1.Thispackageblocksinstallationofdependencieswithknownsecurityissuesbycheckingagainstalistofvulnerableversions.2.Itonlyworksdu

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
