In this chapter
We'll learn how Composer scripts give a project one name for each command, how packages install command-line tools into vendor/bin, how require-dev keeps development tools off the production server, which config settings matter (platform, sort-packages, allow-plugins), and how to install private packages without ever committing a password.
The Problem in Real Life
The README has grown a "Commands" section: php bin/send-reminders.php 2026-11-06, phpstan analyse src --level 6, php -S localhost:8000 -t public, and four more. Mia types the PHPStan one from memory with --level 5; Marcus uses a PHPStan installed globally on his laptop, a year older than hers. They get different results on the same code.
And the deploy script, which runs composer install on the server, has just installed PHPStan and the test tools on the production machine, where they'll never run.
Each command should have one name, one version of each tool, and none of the dev tools on the server.
Marcus
Commands in a README vs. Commands in composer.json
Everyone types it differently
A command copied from a README drifts: other flags, other folders, other versions.
Tools are dependencies too
PHPStan and PHPUnit belong in composer.json, locked to one version for the whole team.
Not on the server
Development tools go in require-dev, and production installs with --no-dev.
Composer Scripts, Binaries and Configuration
composer.json as the project's control panel: besides dependencies and autoloading, composer.json can hold the project's commands, its tools and its settings. This chapter covers:
- Scripts: named commands, and scripts that run on Composer events.
- Binaries and require-dev: tools in
vendor/bin, kept off production. - Configuration: the
configsettings worth knowing. - Private packages: private repositories and auth.json.
Scripts: the scripts section gives commands a name: "reminders": "@php bin/send-reminders.php" lets anyone run composer reminders, and arguments go after --: composer reminders -- 2026-11-07. A script can be a list of commands run in order, and @composer or @php run Composer itself or the same PHP Composer is using, rather than whatever php happens to be first on the system path. "check": ["@composer validate --strict", "@composer audit"] puts two checks behind one name. composer run-script --list shows every script, with the descriptions from scripts-descriptions. The README shrinks to "run composer run-script --list".
{"scripts": {"reminders": "@php bin/send-reminders.php","check": ["@composer validate --strict","@composer audit"],"analyse": "phpstan analyse src --level 6"},"scripts-descriptions": {"reminders": "Print the SMS reminders for a day"}}
$ composer run-script --listscripts:reminders Print the SMS reminders for a daycheck Runs the check script as defined in composer.json$ composer reminders -- 2026-11-06Reminders for 2026-11-06+447700900456 <- BookBay Bike Repairs: hi Tom Hart, see you Fri 6 Nov 09:30. Price £27.50.$ composer check./composer.json is validNo security vulnerability advisories found.
Event scripts: scripts named after a Composer event run automatically: post-install-cmd after composer install, post-update-cmd after composer update, post-autoload-dump after the autoloader is regenerated, and a few more. Frameworks use them to clear caches or publish files after every install; Laravel's composer.json has several. Keep them quick and safe to run twice, because they run on every install, including on the server.
{"scripts": {"post-autoload-dump": ["@php -r \"echo 'autoloader ready', PHP_EOL;\""]}}$ composer dump-autoloadGenerating autoload files> @php -r "echo 'autoloader ready', PHP_EOL;"autoloader readyGenerated autoload files
Binaries in vendor/bin: a package can ship a command-line tool. Composer links it into vendor/bin/ when installing, so after composer require --dev phpstan/phpstan everyone runs vendor/bin/phpstan, at the version in the lock file — not a global install that differs on every laptop. Inside Composer scripts, vendor/bin is added to the path, so a script can say just "analyse": "phpstan analyse src --level 6". Act 21 sets up PHPStan and PHPUnit exactly this way.
require-dev and --no-dev: dependencies only needed while developing — test frameworks, PHPStan, code-style fixers — go under require-dev, added with composer require --dev. They're locked like everything else, so the team shares one version. On production, composer install --no-dev -o skips them all and builds the optimised classmap from chapter 6: a smaller server, and fewer packages that could carry a security hole. The diagram below shows what goes where.
Who gets what: require vs. require-dev
require
giggsey/libphonenumber-for-php-lite, bookbay/pricing
require-dev
phpstan/phpstan, phpunit/phpunit
composer install
laptops and CI: both sections, tools in vendor/bin
composer install --no-dev -o
production: only require, plus an optimised classmap
Configuration: the config section changes how Composer behaves for this project. "sort-packages": true keeps require alphabetical, so two people adding packages don't collide in Git. "platform": {"php": "8.4.0"} makes Composer resolve versions as if PHP were 8.4 — essential when your laptop runs 8.5 but the server runs 8.4, or Composer may lock a package that needs 8.5. "optimize-autoloader": true makes -o the default. "allow-plugins" lists the Composer plugins (packages that extend Composer itself) you trust to run code during installs; Composer asks before running any others. The table below collects these settings.
| Setting | Example | What it does |
|---|---|---|
| sort-packages | true | Keeps require and require-dev alphabetical |
| platform | {"php": "8.4.0"} | Resolves versions as if this PHP version were installed |
| optimize-autoloader | true | Always build the classmap (like -o) |
| allow-plugins | {"vendor/plugin": true} | Which Composer plugins may run code |
| process-timeout | 600 | Seconds before a script is stopped (default 300) |
| vendor-dir | "vendor" | Where packages are installed |
Private packages: some packages aren't public — BookBay's own shared code, or a supplier's library under a licence. Add their Git repository under repositories with type vcs and Composer installs them like any other package, reading versions from the repository's tags. The repository needs a login, and that goes into auth.json, a separate file next to composer.json (or in your home folder) holding tokens per host. Never commit auth.json — add it to .gitignore. On CI and servers, put the same JSON into the COMPOSER_AUTH environment variable instead. Teams with many private packages use a private package server, like Private Packagist or the free, self-hosted Satis.
// ---- composer.json (part) ----{"repositories": [{ "type": "vcs", "url": "https://github.com/bookbay/shared-kernel" }],"require": {"bookbay/shared-kernel": "^1.2"}}// ---- auth.json (in .gitignore!) ----{"github-oauth": {"github.com": "<a personal access token>"}}
Key Takeaway
Composer scripts name a project's commands (composer reminders -- 2026-11-07); @php and @composer use the same PHP and Composer, and event scripts like post-install-cmd and post-autoload-dump run automatically. Packages install their tools into vendor/bin, locked to one version for everyone and on the path inside scripts. Development tools go in require-dev and production installs with --no-dev -o. config holds sort-packages, platform (resolve for the server's PHP version), optimize-autoloader and allow-plugins. Private packages come from vcs repositories, with tokens in auth.json or COMPOSER_AUTH — never committed.
Why This Matters
In a real team, composer.json is the first file a new developer reads: its scripts are the project's commands, its require-dev the team's tools, its config the rules everyone shares. Laravel projects come with scripts and event scripts already in it.
Keeping development tools off production and tokens out of Git are two of the simplest security wins there are — and both are a single line once you know them.
composer run-script --list is the README now, PHPStan is in require-dev at one version, and the server installs with --no-dev. Then Mia adds a logging library for the reminder script: composer require monolog/monolog. Composer refuses with a "Problem 1" about psr/log, a package she has never heard of. And the next morning's composer update stops on Guzzle: "affected by security advisories". That's the next chapter: dependency conflicts and security.
