Runway

Symfony 7.x API

The following describes an example setup for a Symfony 7.x api skeleton.

Install/setup

Create the project with composer:

$ composer create-project symfony/skeleton:"7.1.*" ./symfony7-api
...

Go into the project directory and initialise a git repository:

$ cd symfony7-api
$ git init
Initialized empty Git repository in /home/runway/projects/symfony7-api/.git/

Containerize

The next steps make Symfony aware of the Docker environment, followed by a re-install:

$ composer config --json extra.runtime.disable_dotenv 'true'
$ composer config --json extra.symfony.docker 'true'
$ rm symfony.lock
$ composer recipes:install --force --verbose
...
  * Use git diff to inspect the changes.

    Not all of the changes will be relevant to your app: you now
    need to selectively add or revert them using e.g. a combination
    of git add -p and git checkout -p

  * Use git checkout . to revert the changes.

    New (untracked) files can be inspected using git clean --dry-run
    Add the new files you want to keep using git add
    then delete the rest using git clean --force

During the installation it will prompt you to confirm changes to various files, which we all respond to with y since this is a new application.

Runway

Create an application on Runway:

$ runway app create
INFO    checking login status                        
INFO    created app "improved-geophysics"            
create app improved-geophysics: done
next steps:  
* commit your changes  
* runway app deploy  
* runway open

Create the necessary config (APP_ENV and APP_SECRET):

$ runway app config set APP_ENV=prod APP_SECRET=4fa5a889dd96249ffe000dfa08ca81fd
INFO    configuring app "improved-geophysics"        
configure app improved-geophysics: done
   Name                 Value               
APP_ENV    prod                             
APP_SECRET 4fa5a889dd96249ffe000dfa08ca81fd 

Since we set the configuration on Runway, we can git-ignore the .env file (created by composer/symfony install) and keep it around for local development and to be able to run the bin/console commands.

Remember that any changes to the app config of your application, need to be done with runway app config set/rm since the file is not deployed.

Deployment

Deploy the app with runway app deploy.

If you created a new application, you should see a very verbose 404 error message (“Not Found”).

Add a page

For the next part, it’s best to follow the official tutorial and add a controller and route annotation. In this tutorial, we follow the example and refer to /lucky/number going forward.

Once you added the controller file, you can verify the setting:

❯ ./bin/console router:match /lucky/number


                                                                                                                        
 [OK] Route "app_lucky_number" matches                                                                                  
                                                                                                                        

+--------------+---------------------------------------------------------+
| Property     | Value                                                   |
+--------------+---------------------------------------------------------+
| Route Name   | app_lucky_number                                        |
| Path         | /lucky/number                                           |
| Path Regex   | {^/lucky/number$}sDu                                    |
| Host         | ANY                                                     |
| Host Regex   |                                                         |
| Scheme       | ANY                                                     |
| Method       | ANY                                                     |
| Requirements | NO CUSTOM                                               |
| Class        | Symfony\Component\Routing\Route                         |
| Defaults     | _controller: App\Controller\LuckyController::number()   |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
|              | utf8: true                                              |
+--------------+---------------------------------------------------------+

Add the new file and commit the changes; then move on to the next step.

Web server

By default, Runway will start an Apache web server (with php-fpm) for PHP applications. This is mostly a convenience in order to make .htaccess files work. If you prefer to use nginx, we refer you to the web server docs for customization.

In order to make Symfony serve /lucky/number, we have to add the following to a .htaccess inside the public directory:

Require all granted
FallbackResource /index.php

For the most up-to-date snippet of this, please read the official documentation for the Symfony framework.

Final steps: runway app deploy

… and curl a lucky number:

$ curl https://improved-geophysics.pqapp.dev/lucky/number
<html><body>Lucky number: 25</body></html>

Recap

In this tutorial, we learned the following:

  1. We installed a recent version of the Symfony framework
  2. We configured the framework to run in a container and to understand environment variables.
  3. We configured the Apache web server to serve fancy URLs