将symfony部署到Heroku,通过configvars获取swiftmailer参数

时间:2017-01-25 11:54:30

标签: symfony variables heroku config swiftmailer

我已将我的symfony应用程序部署到Heroku,但我想使用Heroku提供的config_vars配置swiftmailer组件。我已经有两个了:

  1. SYMFONY_ENV:prod
  2. CLEARDB_DATABASE_URL:my-connection-string
  3. 现在我想做的是在config_prod.yml中描述swiftmailer凭据:

    #config_prod.yml
    
    swiftmailer:
        transport: "%env(mail_transport)%"
        host:      "%env(mail_host)%"
        username:  "%env(mail_user)%"
        password:  "%env(mail_password)%"
        spool:     { type: memory }
    

    然后,我在Heroku上设置了config_vars,但是在尝试部署应用程序时,我得到了以下异常。

    [Symfony\Component\DependencyInjection\Exception\EnvParameterException]
    Incompatible use of dynamic environment variables "mail_transport" found in parameters.
    
    [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
    Unable to replace alias "swiftmailer.mailer.default.transport.real" with actual definition "%env(mail_transport)%".
    
    [Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
    You have requested a non-existent service "%env(mail_transport)%".
    
    Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the symfony-scripts event terminated with an exception
    

    如果我在config.yml中对参数进行硬编码,那么部署到Heroku会成功。

    我搞砸了什么? Thx提前。

1 个答案:

答案 0 :(得分:0)

我对heroku知之甚少,但我知道如何在symfony配置中获取ENV_VAR;)

你的其他配置文件所在的app/config内的

会创建一个名为parameters.php的新文件

<?php
if(isset($_SERVER['AWS_MAILER_HOST']) ){
    $container->setParameter('mailer_transport', @$_SERVER['AWS_MAILER_TRANSPORT']);
    $container->setParameter('mailer_host', @$_SERVER['AWS_MAILER_HOST']);
    $container->setParameter('mailer_username', @$_SERVER['AWS_MAILER_USERNAME']);
    $container->setParameter('mailer_password', @$_SERVER['AWS_MAILER_PASSWORD']);
    $container->setParameter('mailer_port', @$_SERVER['AWS_MAILER_PORT']);
    $container->setParameter('mailer_encryption', @$_SERVER['AWS_MAILER_ENCRYPTION']);
    $container->setParameter('mailer_auth_mode', @$_SERVER['AWS_MAILER_AUTH_MODE']);
}

'AWS_MAILER_...'

替换所有ENVIRONEMENT_VAR

app/config/config.yml放置

imports:
    - { resource: parameters.yml }
    - { resource: parameters.php } <------------