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 http://miftyisbored.com/complete-restful-service-client-cakephp-tutorial/ with https://github.com/openshift/cakephp-ex to get https://bitbucket.org/emergile/items.git .
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.
$ $ 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:
- uncomment the last 4 lines of migrate_database.sh and
- 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=https://github.com/$YOUR_FORK_OF_items_REPO/items.git -p DATABASE_USER=user -p DATABASE_PASSWORD=password $ oc status
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 migrate-database.sh. 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) Hostname: 127.0.0.1 Port: 3306 Username: user Password: password
You can then populate the database by a copy/paste of the sql instructions as supplied at: https://bitbucket.org/emergile/items/master/etc/items.sql.
Verify Database Populated
Once the application has been built and populated point your Browser at: http://items-rest.127.0.0.1.xip.io/ and you should see something like below. Click around.
Try some other REST URL invocations, for example:
$ curl http://items-rest.127.0.0.1.xip.io/rest_items.json $ curl http://items-rest.127.0.0.1.xip.io/rest_items/1.xml
This is my very first attempt at using the CakePHP framework so owe much to the sample described at: http://miftyisbored.com/complete-restful-service-client-cakephp-tutorial/.
Visit https://cakephp.org/ for more information including documentation and usage tutorials.