【问题标题】:Why is Symfony missing dev bundles in prod environment?为什么 Symfony 在 prod 环境中缺少开发包?
【发布时间】:2015-05-18 16:59:17
【问题描述】:

我的 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 致命错误:在第 29 行的 /my/project/app/AppKernel.php 中找不到类 'Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle' 脚本 Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache 处理 post-install-cmd 事件以异常终止 [运行时异常] 执行“'cache:clear --no-warmup'”命令时出错。

这不仅仅是 Doctrine Fixtures Bundle 的问题。如果我更改顺序以便 Liip 功能测试包首先出现,那么错误将与该包有关。

为什么我会看到这个错误?为什么 Symfony 尝试访问这些包,即使我们明确不在开发环境中(注意 --no-dev composer 标志)?我可以做些什么来消除这种情况,而不必在生产机器上安装所有开发依赖项?

【问题讨论】:

    标签: php symfony composer-php


    【解决方案1】:

    因为symfony默认的env是devcomposer --no-dev只是告诉composer不要安装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
    

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

    【讨论】:

    • 谢谢,这是问题的核心。不幸的是,没有办法将命令行参数添加到 composer 调用,然后传递给 cache:clear 命令。似乎必须按照 Fracsi 的建议进行。
    猜你喜欢
    • 1970-01-01
    • 2021-11-25
    • 2016-12-25
    • 1970-01-01
    • 2016-07-30
    • 2018-09-23
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    相关资源
    最近更新 更多