Hi there and welcome back to Platform from scratch. Today we’re going to take a very simple Laravel application that will make use of Postgres on the backend as a database.
The complete example application can be found here - https://github.com/JGrubb/platformsh-laravel-example
The very first step of this will be to add in the appropriate .platform/services.yaml
file. This file was left intentionally empty in the setup for this Laravel application, as we didn’t have a need for a working database and were just getting our app set up and running. Now however, we’re going to add a very simple configuration into services.yaml
.
Over in .platform.app.yaml
we’ll add in the relationships section to the file.
Commit this and push it up to your platform remote. This will trigger a rebuild of your application, and when that’s done let’s SSH into your environment with the Platform CLI - $ platform ssh
.
Once you’re in, try this - echo $PLATFORM_RELATIONSHIPS
. You’ll get a big base64 encoded string as a result. This is because you can’t set complex objects or even JSON as an environment variable, so let’s decode that by piping it to base64 --decode
- echo $PLATFORM_RELATIONSHIPS | base64 --decode
This should give you back something like
{"database": [{"username": "main", "password": "main", "ip": "250.0.96.171", "host": "database.internal", "query": {"is_master": true}, "path": "main", "scheme": "pgsql", "port": 5432}]}
.
sidebar
Just for the fun of it, let’s try this in the relationships
section -
Sure enough —
web@4ikq2xigwlw5s-master--app:~$ echo $PLATFORM_RELATIONSHIPS | base64 --decode
{"dark_chocolate": [{"username": "main", "password": "main", "ip": "250.0.96.171", "host": "big_daddy.internal", "query": {"is_master": true}, "path": "main", "scheme": "pgsql", "port": 5432}]}
So the basic gist of how you establish a connection to any kind of service that you set up in services.yaml
should now be clearer than it was before, and we’ll now set about adding the code to our Laravel app that will make use of these environment variables.
This particular piece of code is not Postgres specific, and in fact will work just fine with MySQL as well. The beauties of abstraction…
The final step in this process is optional, but if you want to have artisan
migrate the database on deploy rather than logging into the server and running it manually you’d add this to the bottom of your hooks.deploy
in .platform.app.yaml
—
The --force
flag will allow migrate to run in a “production” environment.
There is one final step to be aware of, and that’s that the pdo_pgsql
extension is not enabled by default in the PHP containers. You’ll need to add this somewhere in .platform.app.yaml
-
If you were using MySQL, this step would not be needed as pdo_mysql
is enabled by default. Indeed, if you’re using Postgres, you can disable the MySQL extension if you wish -
For reference, here’s the complete .platform.app.yaml
—