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

时间:2015-05-18 08:48:59

标签: php symfony composer-php

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

以下是我在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尝试访问这些bundle,即使我们明确没有在dev环境中(注意--no-dev composer标志)?如果不必在生产机器上安装所有dev依赖项,我该怎么做才能消除它?

2 个答案:

答案 0 :(得分:11)

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

E.g: $ SYMFONY_ENV=prod php composer.phar install --no-dev --optimize-autoloader

答案 1 :(得分:3)

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

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

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

相关问题