Platform.sh User Documentation

Symfony integration

Upsun Beta

Access our newest offering - Upsun!

Get your free trial by clicking the link below.

Get your Upsun free trial

Symfony has a special integration with Platform.sh that makes it easier to use Platform.sh for Symfony projects.

When using the Symfony integration, you are contributing financially to the Symfony project.

The Symfony integration is automatically enabled when:

  • You run the symfony new command with the --cloud option;

  • You run symfony project:init on an existing project to automatically generate the Platform.sh configuration.

If you already have a Platform.sh configuration without the Symfony integration, enable it by adding the following configuration:

.platform.app.yaml
 hooks:
     build: |
         set -x -e

         curl -fs https://get.symfony.com/cloud/configurator | bash

         # ...         

The configurator enables the following integration:

Tools Anchor to this heading

The configurator (curl -fs https://get.symfony.com/cloud/configurator | bash) is a script specially crafted for Platform.sh. It ensures that projects are always using the latest version of the following tools:

Hooks Anchor to this heading

The hooks section defines the scripts that Platform.sh runs at specific times of an application lifecycle:

  • The build hook is run during the build process
  • The deploy hook is run during the deployment process
  • The post-deploy hook is run after the deploy hook, once the application container starts accepting connections

Here’s the default hooks section optimized for Symfony projects:

.platform.app.yaml
hooks:
    build: |
        set -x -e

        curl -s https://get.symfony.com/cloud/configurator | bash

        symfony-build        

    deploy: |
        set -x -e

        symfony-deploy        

For more information, see Hooks.

To gain a better understanding of how hooks relate to each other when building and deploying an app, see the Platform.sh philosophy.

symfony-build Anchor to this heading

symfony-build is the script that builds a Symfony app in an optimized way for Platform.sh. Use it as the main build script in your build hook.

symfony-build performs the following actions:

  • Removes the development frontend file (Symfony <4)
  • Installs PHP extensions through the php-ext-install script
  • Installs application dependencies using Composer
  • Optimizes the autoloader
  • Builds the Symfony cache in an optimized way to limit the time it takes to deploy
  • Installs the JavaScript dependencies via npm or Yarn
  • Builds the production assets using Encore

To override the flags used by Composer, use the $COMPOSER_FLAGS environment variable:

.platform.app.yaml
hooks:
    build: |
        set -x -e

        curl -s https://get.symfony.com/cloud/configurator | bash

        COMPOSER_FLAGS="--ignore-platform-reqs" symfony-build        

When installing dependencies, the script automatically detects if the app is using npm or Yarn.

To disable the JavaScript dependencies and asset building, set NO_NPM or NO_YARN to 1 depending on your package manager.

To customize Node/npm/Yarn behaviors, prefix the symfony-build script with the following environment variables:

  • NODE_VERSION: to pinpoint the Node version that nvm is going to install. The default value is --lts.
  • YARN_FLAGS: flags to pass to yarn install. There is no default value.

symfony-deploy Anchor to this heading

Use symfony-deploy as the main deploy script in the deploy hook. It only works if you’re using the symfony-build script in your build hook.

symfony-deploy performs the following actions:

  • Replaces the Symfony cache with the cache generated during the build hook
  • Migrates the database when the Doctrine migration bundle is used

php-ext-install Anchor to this heading

You can use the php-ext-install script to compile and install PHP extensions not provided out of the box by Platform.sh, or when you need a specific version of an extension. The script is written specifically for Platform.sh to ensure fast and reliable setup during the build step.

php-ext-install currently supports three ways of fetching sources:

  • From PECL: php-ext-install redis 5.3.2
  • From a URL: php-ext-install redis https://github.com/phpredis/phpredis/archive/5.3.2.tar.gz
  • From a Git repository: php-ext-install redis https://github.com/phpredis/phpredis.git 5.3.2

To ensure your app can be built properly, run php-ext-install after the configurator but before symfony-build:

.platform.app.yaml
hooks:
    build: |
        set -x -e

        curl -s https://get.symfony.com/cloud/configurator | bash

        php-ext-install redis 5.3.2

        symfony-build        

When installing PECL PHP extensions, you can configure them directly as variables instead:

.platform.app.yaml
variables:
    php-ext:
        redis: 5.3.2

Advanced Node configuration Anchor to this heading

If you need to use the Node installation setup by symfony-build, use the following configuration:

.platform.app.yaml
hooks:
    build: |
        set -x -e

        curl -s https://get.symfony.com/cloud/configurator | bash

        symfony-build

        # Setup everything to use the Node installation
        unset NPM_CONFIG_PREFIX
        export NVM_DIR=${SYMFONY_APP_DIR}/.nvm
        set +x && . "${NVM_DIR}/nvm.sh" use --lts && set -x
        # Starting from here, everything is setup to use the same Node
        yarn encore dev        

If you want to use two different Node versions, use the following configuration instead:

.platform.app.yaml
hooks:
    build: |
        set -x -e

        curl -s https://get.symfony.com/cloud/configurator | bash

        symfony-build

        cd web/js_app
        unset NPM_CONFIG_PREFIX
        export NVM_DIR=${SYMFONY_APP_DIR}/.nvm

        NODE_VERSION=8 yarn-install

        # Setup everything to use the Node installation
        set +x && . "${NVM_DIR}/nvm.sh" use 8 && set -x

        # Starting from here, everything is setup to use Node 8
        yarn build --environment=prod        

Is this page helpful?