REST API in Slim 3 Framework – Part 1


[back to top]

This is a multi-part series for creating a REST service (or services) in the Slim 3 framework. At work I write microservices in Java w/ Spring Boot or Node.j w/ Express.js or Loopback. At home though, I still use PHP since it’s the best option supported by cheap, shared hosting plans. PHP has a number of well seasoned frameworks, but I chose Slim for a few reasons:

  1. Slim 3 is super lightweight.
  2. Slim 3 is fast, in both performance and to develop on.
  3. Slim 3 is standards compliant.
  4. Slim 3 is flexible and can be used as the whole website, just the API or any combination in between.
  5. Many popular packages (like those written for Laravel) also work in Slim 3.
  6. Once upon a time, the Slim framework founder (also founder of PHP The Right Way and author of Modern PHP) was a 3rd party developer for a Mac-only web authoring tool called RapidWeaver. And so was I. It was a small group of developers and we all formed a tight knit community as a result.


[back to top]

To make life easy — and the environment guaranteed to be consistent — we are going to use a headless virtual machine for development. But not just any virtual machine, we’re going to use the well respected Homestead, commonly used for Laravel development. By using Homestead, we have all the required tools installed and configured (PHP, nginx, databases, git, caches and more) without all the mess, and we get it all on a repeatable, isolated operating system.

To run Homestead we are going to use Vagrant, which in turn controls VirtualBox. The extend to which you will interact with either one of these is minimal (unless debugging either one) so if you’re not comfortable with them or have not used either in the past then don’t fret too much. Here’s how they all connect — Homestead is a pre-configured operating system designed to run PHP web projects, VirtualBox is the virtualisation software that Homestead will run on, and Vagrant is the software that gives us an easy set of configurations and commands to run Homestead on top of VirtualBox with ease.

Once we have these three technologies set up, you literally need to know vagrant up and vagrant halt to start and stop your development environment.

We’re also going to use Composer to manage dependencies and get the application skeleton stood up. In order to use Composer we first need to install at least one version of PHP (if not already installed on your system).

Lastly, before we start, my instructions assume you are using Ubuntu 16.04 or later. You can use Mac or Windows just as well, I just don’t provide instructions for either one.

Let’s get started.

Install PHP

[back to top]

I’m installing PHP7, which is default in the Ubuntu repositories since 16.04, but anything greater that PHP5.5 should do. In a terminal window:

$ sudo apt install php php7.1-xml php-mbstring

Now test that php is installed by using the following in a terminal window:

$ php -v

PHP 7.1.15-0ubuntu0.17.10.1 (cli) (built: Mar 14 2018 22:30:42) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.1.15-0ubuntu0.17.10.1, Copyright (c) 1999-2018, by Zend Technologies

Install Composer

[back to top]

Now that we have PHP installed, we can use it to run Composer, our dependency manager. You can run Composer on a per-project basis (i.e. locally in your project), but you usually use Composer a lot in a PHP project so it’s handy to install it globally.

  1. Head over to the Download page instructions and copy and paste the 4 lines of code found there into a terminal window, in your Downloads folder, for instance.
  2. To make Composer a global command, move the composer.phar to your path:
    $ sudo mv composer.phar /usr/local/bin/composer

To test that Composer is installed, enter the following in a terminal window:

$ composer -v

  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
Composer version 1.6.4 2018-04-13 12:04:24

  command [options] [arguments]

  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version

Install VirtualBox

[back to top]

First things first, make sure your computer is set up to handle virtualisation properly. This means going into the BIOS and ensuring that hardware excelleration is enabled (in some cases) and that any virtualisation feature is enabled. You will struggle through countless hours of debugging if you don’t prepare your BIOS first.

To install VirtualBox on Linux, don’t use the version found in the Ubuntu software repositories as it is too old for Homestead. What you need to do is grab the latest AMD64 .deb that matches your distribution from the VirtualBox download page. Once downloaded, use GDebi Package Installer to install. Ignore the warning that a better supported version can be found in the repo.

To make sure VirtualBox installed correctly, check your app drawer or run virtualbox in the terminal. This is likely the last time you’ll see the VirtualBox GUI since we’ll be using it headless from here on out.

Install Vagrant

[back to top]

The purpose of Vagrant is to provide work environments that are easy to configure, reproducible, and portable. That’s exactly what Homestead is. It’s a whole PHP developer workstation in a box. A Vagrant box, distributed on the internet and everyone using it gets the exact same experience. Vagrant allows us to interact with it by starting and stopping it and shelling into it, but it also allows us to configure it as needed.

To install Vagrant, go to the download page and download the 64-bit .deb (if you’re on Linux). Once again, use GDebi Package Installer to install the application.

Once installed you can run the following to make sure Vagrant was installed correctly:

$ vagrant -v
Vagrant 2.0.4

Continue reading on the next page…

Pages: 1 2 3