Using ShimmerCat with PHP

PHP + ShimmerCat

Here is a simple application that talks to a PHP backend:

This is a very basic PHP application. To run it, open a couple of terminals in the folder that you get after uncompressing the .zip file (i.e., the folder where the README file is) and in one of the terminals execute shimmercat devlove, while in the other execute your favourite PHP script host. Examples of the later would be:

php-cgi  -b 127.0.0.1:8095

or

hhvm -m server -p 8095 -d hhvm.server.source_root=`pwd`/backend

or if you want to use FastCGI with HHVM:

hhvm --m server -d hhvm.server.type=fastcgi -d hhvm.server.port=8095

Of course, it's also possible to use PHP-FPM, but we don't cover it here because PHP-FPM always requires long, well-thought configuration files that deserve their own series of articles.

In all the cases, ShimmerCat will detect automatically if the PHP backend is talking FastCGI or HTTP/1.1 (unless the protocol is indicated in the devlove.yaml). This detection however is only made the first time that the server can successfully connect to the backend. If after that the protocol of the backend changes without restarting ShimmerCat, you will likely get a screen like the one below:

Image

Notes

The example above is made using electric domains and the re-write engine.

Frontend assets and PHP files are separated.

A little bit unconventionally for PHP, we put backend files in a different folder than frontend files. We find that this is more secure than mixing them, since it makes a lot harder to just fetch the PHP sources of the application by unauthorized agents. However, this separation is not compulsory; it is possible to set root-dir and document-root to point to the same directory.

In the example above, CSS, Javascript and image files should be put as usual below the root-dir.

Access to PHP files needs to be authorized

Notice the use of the change-url directives in both the devlove.yaml file and the frontend/views/__index.html file. They work together to route requests to / to a special PHP file that acts as a view.

The file frontend/views/__index.html will route any requests of the form /api/views/xxx/ to a PHP file named xxx.php in the backend/views/ folder. See our guide on handling application contents for more details on this. However, it is not possible to direct requests to PHP files outside that folder, at least without additional rules. The result is an authorization schema for indicating which PHP files can be accessed in the application. This also helps with application security.

Examples:

Basic-php-landing:

This is a very basic PHP application. To run it, open a couple of terminals in the folder that you get after decompressing the .zip file (i.e., the folder where this README is) and in one of the terminals execute shimmercat devlove, while in the other execute your favourite PHP script host. Examples of the later would be:

php-cgi  -b 127.0.0.1:8095

or (with Facebook's PHP engine):

hhvm -m server -p 8095 -d hhvm.server.source_root=`pwd`/backend

or

hhvm --m server -d hhvm.server.type=fastcgi -d hhvm.server.port=8095

It's also possible to use PHP-FPM, provided that you know how to configure it.

Files:

---
shimmercat-devlove:
    domains:
        elec www.myphpsite.com:
            root-dir: frontend
            consultant:
                document-root: backend
                connect-to: "8095"
            change-url:
                - / -> /api/views/landing_page/
<?php
print("Hello, you have just landed");
?>
<!--
shimmercat:
   content-disposition: replace
   change-url:
      - /api/views/<view_name>/ -> /views/<view_name>.php
-->