Symfony2 没有使用 config_prod.yml

Symfony2 is not using the config_prod.yml

我有一个 symfony2 项目,但我无法使用我的 config_prod.yml。 我在 parameters.yml 中设置了 2 个数据库主机。其他用于在本地主机中开发,其他用于生产。我相应地在 config_dev.yml 和 config_prod.yml 中调用了这些参数,但产品中的参数不起作用。它总是使用 config_dev.yml 中的数据库,如果我将 prod -database url 放入 config_dev.yml,它会连接到那里。不管我在 config_prod.yml 中输入什么,它都不会使用这些值。

这是我的 config_dev.yml:

imports:
    - { resource: config.yml }

framework:
    router:
        resource: "%kernel.root_dir%/config/routing_dev.yml"
        strict_requirements: true
    profiler: { only_exceptions: false }

web_profiler:
    toolbar: true
    intercept_redirects: false

assetic:
    use_controller: true

doctrine:
    dbal:
        host: "%database_host_local%"

这是我的 config_prod.yml:

imports:
    - { resource: config.yml }

doctrine:
    dbal:
        host: "%database_host_prod%"

(其余数据库值在 config.yml 中设置,因为我现在只想更改主机。)

这是parameters.yml(当然所有值都改变了):

parameters:
    database_driver: pdo_mysql
    database_host_local: localhost
    database_host_prod: 11.11.11.11 # <-- This is never used if called in the config_prod.yml but IS used when called from the config_dev.yml
    database_port: 3306
    database_name: mydb
    database_user: user
    database_password: pass
    database_charset: utf8mb4

最后是我用来更改环境的 .htaccess 文件:

RedirectMatch permanent ^/app\.php/(.*) /

DirectoryIndex app.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # Redirect to https-site:
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

    # Explicitly disable rewriting for front controllers
    RewriteRule app_dev.php - [L]
    RewriteRule app.php - [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    # Change below before deploying to production
    RewriteRule ^(.*)$ app.php [QSA,L]
    #RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

除了添加包外,我没有接触过 AppKernel。

为什么 symfony 不使用 config_prod.yml?

编辑:app.php(第一)和 AppKernel.php(第二)

<?php

use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';

// Enable APC for autoloading to improve performance.
// You should change the ApcClassLoader first argument to a unique prefix
// in order to prevent cache key conflicts with other applications
// also using APC.
/*
$apcLoader = new ApcClassLoader(sha1(__FILE__), $loader);
$loader->unregister();
$apcLoader->register(true);
*/

require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);

// When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter
//Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
Request::setTrustedProxies(array('127.0.0.1', $request->server->get('REMOTE_ADDR')));
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

AppKernel.php:

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new AppBundle\AppBundle(),
            new OldSound\RabbitMqBundle\OldSoundRabbitMqBundle(),
            new FOS\RestBundle\FOSRestBundle(),
            new FOS\UserBundle\FOSUserBundle(),
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
        }

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }

    public function __construct($environment, $debug)
    {
        date_default_timezone_set( 'Area/City' ); //<-- Changed the timezone from mine.
        parent::__construct($environment, $debug);
    }
}

也许你应该试试别的方法。所以通常 parameters.yml 是从 parameters.yml.dist 文件生成的本地文件。 dist 文件是一个模板,作曲家更新程序在其中创建值并将其写入 parameters.yml。因此最好从您的存储库中排除 parameters.yml。并更改您需要的那台机器上的值。

例如,如果您在登台系统上,则将您的登台数据库写入参数文件。如果你在你的开发系统上,那么将你的开发数据库写入你的配置文件。然后你不需要为每个环境指定一个主机,你可以在本地使用你的文件。

就我而言,我有另一个用于所有单元测试的数据库。当我像这样覆盖参数时,我很容易覆盖它们。在config_test.yml

imports:
    - { resource: config_dev.yml }

parameters:
    database_name: "%database_name_unittest%"

效果很好。最后一个值被合并到您的参数并被替换。

我刚弄明白这个。真的很简单,但很容易忘记。 我正在使用 rabbitmq 进行消息传递,我在后台是 运行 rabbitmq 消费者。这些消费者不时从队列中接收消息,但这些消息永远不会通过 app.php 或 app_dev.php,因为消费者只是后台进程 运行!愚蠢的我没有想到这一点。

消费者使用这样的控制台命令启动:

php app/console rabbitmq:etc..

文档说明如下:

By default, console commands run in the dev environment and you may want to change this for some commands.

所以解决方案是这样启动消费者:

php app/console --env=prod rabbitmq:etc..