Symfony 3.0控制台识别环境变量

时间:2016-06-14 17:40:07

标签: php console doctrine symfony

我在vhost上设置了一些环境变量:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog /home/pcmagas/Kwdikas/web/apps/logs/error.log
    CustomLog /home/pcmagas/Kwdikas/web/apps/logs/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf

    Alias /symphotest /home/pcmagas/Kwdikas/web/apps/symphotest/web
    <Directory /home/pcmagas/Kwdikas/web/apps/symphotest/web>
        AllowOverride All
        Require all granted

        DirectoryIndex appp_dev.php
    </Directory>


    <Directory /home/pcmagas/Kwdikas/web/apps/symphotest>
    SetEnv OPENSHIFT_POSTGRESQL_DB_HOST localhost
    SetEnv OPENSHIFT_POSTGRESQL_DB_PORT 5432
    SetEnv OPENSHIFT_APP_NAME sampledb
    SetEnv OPENSHIFT_POSTGRESQL_DB_USERNAME sampleuser
    SetEnv OPENSHIFT_POSTGRESQL_DB_PASSWORD samplepasswd
    </Directory>
</VirtualHost>

此外,我都将环境变量导出到shell:

pcmagas@pcmagas-Lenovo-G70-70:~/Kwdikas/web/apps/symphotest$ export | grep OPENSHIFT
declare -x OPENSHIFT_APP_NAME="sampledb"
declare -x OPENSHIFT_POSTGRESQL_DB_HOST="localhost"
declare -x OPENSHIFT_POSTGRESQL_DB_PASSWORD="samplepasswd"
declare -x OPENSHIFT_POSTGRESQL_DB_PORT="5432"
declare -x OPENSHIFT_POSTGRESQL_DB_USERNAME="sampleuser"

我通过定制的环境变量配置数据库连接: config.yml:

imports:
    - { resource: params.php }
    - { resource: security.yml }
    - { resource: services.yml }

# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: en

framework:
    #esi:             ~
    #translator:      { fallbacks: ["%locale%"] }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    #serializer:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
        handler_id:  session.handler.native_file
        save_path:   "%kernel.root_dir%/sessions/%kernel.environment%"
    fragments:       ~
    http_method_override: true
    assets: ~

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #     path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    encryption: "%mailer_encryption_method%"
    port: "%mailer_port%"
    spool:     { type: memory }


<?php
  $container->setParameter('secret','himitsu');

  // Db Parameters
  $container->setParameter('database_driver','pdo_pgsql');
  $container->setParameter('database_host', getenv("OPENSHIFT_POSTGRESQL_DB_HOST"));
  $container->setParameter('database_port', getenv("OPENSHIFT_POSTGRESQL_DB_PORT"));
  $container->setParameter('database_name', getenv("OPENSHIFT_APP_NAME"));
  $container->setParameter('database_user', getenv("OPENSHIFT_POSTGRESQL_DB_USERNAME"));
  $container->setParameter('database_password', getenv("OPENSHIFT_POSTGRESQL_DB_PASSWORD"));

//我已经设置了一些额外的参数,但现在无所谓 ?&GT;

当我输入:

php ./bin/console doctrine:schema:create

我收到以下错误:

  [Doctrine\DBAL\Exception\ConnectionException]                                                  
  An exception occured in driver: SQLSTATE[08006] [7] FATAL:  database "user=''" does not exist  



  [Doctrine\DBAL\Driver\PDOException]                            
  SQLSTATE[08006] [7] FATAL:  database "user=''" does not exist  



  [PDOException]                                                 
  SQLSTATE[08006] [7] FATAL:  database "user=''" does not exist  


doctrine:schema:create [--dump-sql] [--em [EM]] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>

您是否有任何想法如何管理控制台以阅读环境变量?

1 个答案:

答案 0 :(得分:0)

我必须为数据库用户设置密码。

我忘了用数据库密码正确设置数据库,因此我收到连接错误。