禁用 flex recipe 自动执行的两种可靠方式是 --no-scripts 和 symfony_skip_recipes=1:前者禁用所有 composer 脚本(含 recipe),后者仅跳过 recipe 执行且保留其他脚本;推荐优先使用后者以实现细粒度控制。

禁用 Flex recipe 自动执行的两种可靠方式
Flex 在 composer require 时默认拉取并应用 recipe,想跳过它,不能只靠 --no-interaction——这个参数只抑制交互提示,不阻止 recipe 执行。真正起效的是两个独立开关:--no-scripts 和 SYMFONY_SKIP_RECIPES=1。
-
--no-scripts:Composer 原生命令行选项,直接禁用所有scripts(包括 Flex 的auto-scripts),recipe 不会触发,但也会关掉其他脚本(如assets:install) -
SYMFONY_SKIP_RECIPES=1:Flex 专用环境变量,精准跳过 recipe 解析与执行,保留其他 Composer 脚本正常运行 - 两者可共存,但通常只需其一;推荐优先用
SYMFONY_SKIP_RECIPES=1,更细粒度
在 composer.json 中永久关闭 recipe(非全局)
如果某个包你永远不想让它走 recipe(比如自建 bundle 或测试依赖),可在 composer.json 的 extra.symfony.allow-contrib 下补充 skip-recipes 列表:
"extra": {
"symfony": {
"allow-contrib": true,
"skip-recipes": [
"myvendor/my-bundle",
"doctrine/doctrine-migrations-bundle"
]
}
}
注意:skip-recipes 只对列表中明确写出的包生效;路径必须与 Packagist 上的 vendor/name 完全一致;修改后需重新 composer require 才会生效(已安装的 recipe 不会回滚)。
为什么 --no-interaction 不能禁用 recipe?
因为 recipe 执行不依赖用户输入——Flex 在后台自动读取 manifest.json、匹配配置模板、写入 config/ 和 src/,全程无 prompt。常见误解是看到「Installing recipes」日志就以为能按回车跳过,其实它根本没等你按。
- 典型错误现象:
composer require some-bundle --no-interaction后仍看到Writing config/packages/some_bundle.yaml - 根本原因:Flex 的 recipe 钩子注册在
post-package-install脚本里,--no-interaction对脚本执行无影响 - 验证方式:临时加
"scripts": {"post-package-install": "echo 'hook fired'"},再跑--no-interaction就明白
recipe 被跳过时,你得手动补什么?
跳过 recipe 不等于跳过配置——它只是不帮你生成默认文件。如果你真需要那个 bundle,还得自己补关键项:
- 多数 bundle 至少要加一条
App\Kernel::configureContainer()里的$container->loadFromExtension(...)或对应 YAML 配置 - 部分 bundle(如
symfony/mailer)依赖MAILER_DSN环境变量,recipe 会自动加到.env,跳过后得手填 - 别漏掉
config/bundles.php:Flex recipe 默认帮你注册 bundle,跳过后要手动加MyVendor\MyBundle\MyBundle::class => ['all' => true]










