Deploying Symfony-CMF project to Heroku

2019-08-30 22:47发布

问题:

I currently following CMF docs to create a project : https://symfony.com/doc/master/cmf/tutorial/introduction.html

As i continue on Tutorial, i like to push the project to heroku. But i ran in to a problem when i need to have database connection.

Than i found this source : https://coderwall.com/p/qpitzq/deploing-symfony-project-using-mysql-to-heroku

That helped but i need to configure also 'phpcr_backend' parameters. I set them on console:

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

and update parameters_production.php file:

<?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'));

Now, when i deploy the project im getting this error :

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

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

Im not sure, im setting those parameters on correct syntax. For now this is the problem.

Edit:

I hard coded doctrine_phpcr parameters in config.yml file :

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%"

Currently error is

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   

Edit 2:

By changing php version in composer.json, fixed the last issue. Now, when i deploy the project build success. But having another problem:

 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 

Your help would be appreciated

回答1:

Step by step instructions for the sandbox.

First, clone:

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

Then declare optional extensions as required:

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"

Database setup

Map (JAWSDB_|CLEARDB_)?DATABASE_URL in composer.json:

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",

Update config.yml and config_prod.yml with database_url details:

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%'

... and update and commit:

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"

Logging

Update config_prod.yml with logging details:

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:

...and commit:

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

The PHPCR config needs to be in the 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"

Create a Procfile:

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

deploy

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

init DB

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"

done!

heroku open