RESTful APIs: Quick API development with PHP and SLIM

José Antonio Pérez
Apr 13 · 2 min read
Photo by Kevin Ku on Unsplash

In our previous article we introduced a project where one of the goals was to rewrite some legacy programs in the form of frontend and backend components. The backend components had to be implemented as RESTful APIs.

Once solved the problem of getting installed the PDO_OCI extension using the latest currently available version of the Oracle Instant Client driver (18.5) into a recent version of PHP (7.2), the next step is the creation of the API itself.

For the creation of the API we are using the SLIM framework. This is a really lightweight framework including all the necessary features for the creation of RESTful web services. We install the framework using composer, the most popular package manager for PHP.

$ sudo apt install -y composer
$ composer create-project slim/slim-skeleton oracle-pdo-api
$ cd oracle-pdo-api/

With these simple commands we already have all the application scaffolding. The next step is to add some missing components and finally, to write our specific fragments of code.

Then we add the missing components, phpdotenv a package to ease dealing with environment vars (very useful if you are planning to deploy your APIs into any cloud infrastructure), and cors-middleware, a piece of software very helpful for dealing with CORS requests.

$ composer require vlucas/phpdotenv tuupola/cors-middleware

The last step is to write the routing section according to the endpoints being offered by your API, as well as the specific code for connecting and querying to the backend database.

Once finished, you can easily build a Docker container image and then deploy that image with a couple of simple commands:

$ docker build -t “oracle-pdo-api” .
$ docker run — rm -d -p 9020:80 oracle-pdo-api

Congratulations! You RESTful API acting as a gateway to your Oracle database is up and running.

The complete version of the oracle-pdo-api project is available on GitHub

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade