Symfony2未使用config_prod.yml

时间:2016-08-15 18:23:13

标签: php .htaccess symfony config

我有一个symfony2 -project,但我无法使用我的config_prod.yml。 我在parameters.yml中设置了2个数据库主机。其他用于devhost在localhost和其他生产。我相应地在config_dev.yml和config_prod.yml中调用这些参数,但prod中的那个参数不起作用。它始终使用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

最后这是我用来改变envs的.htaccess文件:

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

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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

因此,例如,如果您在临时系统上,则将临时数据库写入参数文件。如果您使用的是dev系统,那么请将您的dev数据库写入配置文件。然后,您不需要为每个环境指定主机,您可以在本地使用您的文件。

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

imports:
    - { resource: config_dev.yml }

parameters:
    database_name: "%database_name_unittest%"

这很好用。最后一个值合并到您的参数并替换。