将 Symfony-CMF 项目部署到 Heroku

Deploying Symfony-CMF project to Heroku

我目前正在按照 CMF 文档创建项目:https://symfony.com/doc/master/cmf/tutorial/introduction.html

在继续学习教程时,我喜欢将项目推送到 heroku。但是当我需要连接数据库时,我 运行 遇到了问题。

然后我找到了这个来源:https://coderwall.com/p/qpitzq/deploing-symfony-project-using-mysql-to-heroku

这很有帮助,但我还需要配置 'phpcr_backend' 参数。我在控制台上设置它们:

heroku config:set phpcr_backend=[type:doctrinedbal,connection:default]
or
heroku config:set phpcr_backend=(type:doctrinedbal,connection:default)
or 
heroku config:set phpcr_backend={type:doctrinedbal,connection:default}
or
heroku config:set phpcr_backend=type:doctrinedbal,connection:default

heroku config:set phpcr_workspace=default
heroku config:set phpcr_user=admin
heroku config:set phpcr_pass=admin

并更新 parameters_production.php 文件:

<?php
$db = parse_url(getenv('...'));
...
$container->setParameter('phpcr_backend', getenv('phpcr_backend'));
$container->setParameter('phpcr_workspace', getenv('phpcr_workspace'));
$container->setParameter('phpcr_user', getenv('phpcr_user'));
$container->setParameter('phpcr_pass', getenv('phpcr_pass'));

现在,当我部署项目时出现此错误:

           [Symfony\Component\Config\Definition\Exception\InvalidTypeException]                                        

       Invalid type for path "doctrine_phpcr.session.sessions.default.backend". Expected array, but got boolean    

我不确定,我正在根据正确的语法设置这些参数。现在这就是问题所在。

编辑:

我在 config.yml 文件中硬编码了 doctrine_phpcr 参数:

doctrine_phpcr:
   # configure the PHPCR session
   session:
       backend: { type: doctrinedbal, connection: default}
       workspace: default
       username: admin
       password: admin
    # enable the ODM layer
   odm:
       auto_mapping: true
       auto_generate_proxy_classes: "%kernel.debug%"

当前错误是

PHP Fatal error:  Cannot use 'String' as class name as it is reserved in /tmp/build_4d5c173733f27d9fb1cec775f9522884/ersah123-cmf-testing-118c5df/vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Mapping/Annotations/String.php on line 32   

编辑 2:

通过更改 composer.json 中的 php 版本,修复了最后一个问题。现在,当我部署项目构建成功。但是还有一个问题:

 heroku run php bin/console doctrine:database:create


 [Doctrine\DBAL\Exception\ConnectionException]                                                                                   
  An exception occured in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known 

您的帮助将不胜感激

沙盒的分步说明。

首先,克隆:

git clone https://github.com/symfony-cmf/cmf-sandbox.git
cd cmf-sandbox
composer install

然后根据需要声明可选扩展:

php -dmemory_limit=4G $(which composer) require "ext-gd:*" "ext-exif:*"
git add composer.json composer.lock
git commit -m "require gd and exif extensions"

数据库设置

在 composer.json 中映射 (JAWSDB_|CLEARDB_)?DATABASE_URL

diff --git a/composer.json b/composer.json
index 0d880da..07a3ba8 100644
--- a/composer.json
+++ b/composer.json
@@ -91,7 +91,9 @@
         "incenteev-parameters": [
             {
                 "file": "app/config/parameters.yml",
-                "env-map": {}
+                "env-map": {
+                    "database_url": "DATABASE_URL"
+                }
             },
             {
                 "file": "app/config/phpcr.yml",

使用 database_url 详细信息更新 config.ymlconfig_prod.yml

diff --git a/app/config/config.yml b/app/config/config.yml
index 3075825..0685fda 100644
--- a/app/config/config.yml
+++ b/app/config/config.yml
@@ -55,13 +55,6 @@ swiftmailer:
 # for jackalope-doctrine-dbal
 doctrine:
     dbal:
-        driver:   '%database_driver%'
-        host:     '%database_host%'
-        port:     '%database_port%'
-        dbname:   '%database_name%'
-        user:     '%database_user%'
-        password: '%database_password%'
-        path:     '%database_path%'
         charset:  UTF8

 # cmf configuration
diff --git a/app/config/config_prod.yml b/app/config/config_prod.yml
index aa51fbf..b704da2 100644
--- a/app/config/config_prod.yml
+++ b/app/config/config_prod.yml
@@ -17,3 +17,7 @@ monolog:
             type:  stream
             path:  '%kernel.logs_dir%/%kernel.environment%.log'
             level: debug
+
+doctrine:
+    dbal:
+        url: '%database_url%'

...并更新并提交:

php -dmemory_limit=4G $(which composer) update --lock
git add composer.json composer.lock app/config/config.yml app/config/config_prod.yml
git commit -m "map DATABASE_URL"

日志记录

使用日志详细信息更新 config_prod.yml

diff --git a/app/config/config_prod.yml b/app/config/config_prod.yml
index b704da2..755cff9 100644
--- a/app/config/config_prod.yml
+++ b/app/config/config_prod.yml
@@ -15,7 +15,7 @@ monolog:
             handler:      nested
         nested:
             type:  stream
-            path:  '%kernel.logs_dir%/%kernel.environment%.log'
+            path:  'php://stderr'
             level: debug

 doctrine:

...并提交:

git add app/config/config_prod.yml
git commit -m "log to stderr in prod"

PHPCR 配置需要在 repo 中:

cp app/config/phpcr_doctrine_dbal.yml.dist app/config/phpcr.yml
sed -i '' '/phpcr.yml/d' .gitignore
git add app/config/phpcr.yml
git commit -m "PHPCR config"

创建 Procfile:

echo 'web: $(composer config bin-dir)/heroku-php-apache2 web/' > Procfile
git add Procfile
git commit -m "Heroku Procfile"

部署

heroku create
heroku config:set SYMFONY_ENV=prod
heroku addons:create heroku-postgresql
git push heroku master

初始化数据库

heroku run "php app/console doctrine:phpcr:init:dbal --force"
heroku run "php app/console doctrine:phpcr:workspace:create default"
heroku run "php app/console doctrine:phpcr:repository:init"
heroku run "php app/console -v -n doctrine:phpcr:fixtures:load"

完成!

heroku open