首页 文章

为什么Symfony在prod环境中缺少dev bundle?

提问于
浏览
7

我的Symfony应用程序有一些依赖项,只有开发,测试等才需要 . 这些是在 require-dev 部分的 composer.json 中定义的 .

以下是我在 AppKernel.php 中添加它们的方法:

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            // ...
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
            $bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
            $bundles[] = new Liip\FunctionalTestBundle\LiipFunctionalTestBundle();
        }

        return $bundles;
    }
}

当我更新我的应用程序时,我运行 php composer.phar install --no-dev --optimize-autoloader . 这将安装开发环境不需要的所有要求,然后清除缓存 .

但是,清除缓存失败,并显示以下消息:

PHP Fatal error:  Class 'Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle' not found in /my/project/app/AppKernel.php on line 29
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception



  [RuntimeException]
  An error occurred when executing the "'cache:clear --no-warmup'" command.

这不仅是Doctrine Fixtures Bundle的一个问题 . 如果我更改顺序,那么Liip Functional Test Bundle会首先出现,那么错误将是关于该捆绑包的 .

为什么我看到这个错误?为什么Symfony尝试访问这些捆绑包,即使我们明确没有在开发环境中(请注意 --no-dev composer标志)?如果不必在 生产环境 机器上安装所有dev依赖项,我该怎么办呢?

2 回答

  • 10

    这是因为symfony默认env是 devcomposer --no-dev 只告诉作曲家不要安装dev要求,symfony不了解环境 . 使用 SYMFONY_ENV=prod 环境变量 . http://symfony.com/doc/current/cookbook/deployment/tools.html#c-install-update-your-vendors

    例如: $ SYMFONY_ENV=prod php composer.phar install --no-dev --optimize-autoloader

  • 2

    您需要告诉 cache:clear 命令在 生产环境 环境中运行 .

    php app/console --env=production cache:clear
    

    (如果有必要,将“ 生产环境 ”更改为您正在处理的特定非开发环境中的任何内容)

相关问题