The Kitchen Cake with OpenShift

With OpenShift you can have your Cake and PHP too. And templates, REST services, side-car pod patterns, database connectivity, runtime environment variable injection, pod lifecycle event hooks, port-forwarding and more. Its every thing and the kitchen sink. But let’s stop shall we before we mangle anymore metaphors. Over to you DeeDee. This lab is another in the OpenShift MiniLabs series.



To create a CakePHP application called items I have mashed up and refactored  with to get .

The result is a REST service that can perform CRUD operations on a (remote) database table known as items – and, moreover, showcase the features called out in the Introduction. This will later be used in a later lab to populate the trivia data for the Cat-of-the-Day (COTD) application using REST calls.



Initial Attempt

This tutorial assumes you have completed the OpenShift MiniLabs installation procedure. Then refresh before continuing. Fork your own copy of the repo.

$ $ cd ~/containersascode
$ ./oc-cluster-wrapper/oc-cluster down
$ ./oc-cluster-wrapper/oc-cluster up containersascode


Create CakePHP Application

Set up a new project and create a new application as we have done before but this time invoking the CakePHP with MySQL template.

But before proceeding, the items repo is used in a variety of scenarios so we need to change and commit some files in your forked repo of items.git:

  1. uncomment the last 4 lines of and
  2. change the name of items/app/Config/schema-items.php to schema.php.
$ cd ~/containersascode
$ oc login -u developer -p developer
$ oc new-project rest --display-name='Items REST Service' --description='Items REST Service'
$ oc project rest
$ oc new-app --template=cakephp-mysql-example -p NAME='items' -p SOURCE_REPOSITORY_URL=$YOUR_FORK_OF_items_REPO/items.git -p DATABASE_USER=user -p DATABASE_PASSWORD=password
$ oc status

Lifecycle Hooks

The application requires a table in the MySQL database named “items”. This is created via a pre hook during the deployment of the front-end PHP application which calls a script called The PHP container locates the MySQL Server back-end side-car container via environment variables. You can inspect this CakePHP configuration detail in the git repository at app/Config/database.php. The pre hook trigger configuration can be viewed as per below.

$ oc login -u developer -p developer
$ oc project rest
$ oc get dc/items -o json | more

We still need to populate the database. CakePHP aficionados may want to edit the CakePHP Config/schema.php and code in some INSERT commands. But in order to illustrate another feature let’s do the following instead.

Database Administration using Port Forwarding

Now for some forwarding fun to populate the database. Let’s point MySQL Workbench at our new database instance so we can explore it using a graphical tool. First setting up port-forwarding as follows:

$ oc login -u developer -p developer 
$ export PODID=`oc get pods -n rest --no-headers=true | grep mysql | cut -f 1 -d ' '`
$ oc port-forward $PODID 3306:3306

Now launch your favourite database administration tool, such as e.g. MySQL Workbench and create a new connection with attributes as per below. Click to connect and you are good to go.

Name: Items
Connection method: Standard (TCP/IP)
Port: 3306
Username: user
Password: password

You can then populate the database by a copy/paste of the sql instructions as supplied at:

Screen Shot 2016-12-03 at 3.26.25 pm.png

Verify Database Populated

Once the application has been built and populated point your Browser at: and you should see something like below. Click around.


Verify Success

Try some other REST URL invocations, for example:

$ curl
$ curl


This is my very first attempt at using the CakePHP framework so owe much to the sample described at:

Visit for more information including documentation and usage tutorials.

Leave a Reply