Tag Archives: Slim

REST API in PHP (on Apache)

Let’s suppose you find yourself in a situation where you have to use PHP (because you’ve got an Apache server you have to work with). Coming from a tomcat and nodejs backend, what’s the closest thing one can do to make this a more familiar and easier to work with architecture?

I introduce the Slim Framework. It’s very easy to use, and if you are familiar with nodejs and express, you’ll find this familiar.

Slim suggests you install them using Composer. Being more of a programmer than IT guy, I don’t like to have to install an util to install another util. That makes my code less portable because if I move it, I’ll have to remember to tell the IT guy what dependencies to include for me. I like my code to be drop-in-ready.

Instead, I suggest you just download their latest release, unzip, and copy their Slim folder and index.php into your webapp.

I also suggest you create a folder called /rest in your webapp root. So your folder should look something like this

/htdocs
  /rest
    /index.php
    /Slim
      /Slim.php
      /Router.php
      ...

SUPER IMPORTANT STEP!!!!!
They won’t highlight this because it’s Apache-specific, but now create a .htaccess file in the /rest folder.

The contents of .htaccess should be

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Don’t worry too much about what this means. It’s basically rewriting the url when clients request the index.php file so that it can translate them to REST paths.

So that’s it!

On yeah, the code.

Here’s a sample

<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$app->contentType("application/json");

$app->get('/resource/:name','getResource');

function getResource($name) {
  $resource = array(
    'name' => $name
    );
  echo json_encode($resource);
}

$app->run();
?>

Let’s go over some pieces…

require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();

This is just some required code to initialize the framework. This is specific to the download-unzip method of installing Slim. (There’s a different way of initializing if you installed using Composer.)

$app = new \Slim\Slim();
$app->contentType("application/json");

The first line creates an instance of Slim.
The next line is optional, which sets ALL responses to be of type json.
You could also include it just within each routing function to specify the content type for just those routes.

$app->run();

This starts the service..

$app->get('/resource/:name','getResource');

function getResource($name) {
  $resource = array(
    'name' => $name
    );
  echo json_encode($resource);
}

This is really the meat of the REST app. This should look very familiar to you if you’ve used node/express before.

You just write $app->get (or $app->post, $app->delete…). The first argument is the path. Notice that :name refers to a variable whose value is passed in as a parameter to the function. The following argument(s) are callables or functions. I can also give it a name of a function as I have done here.
I say the “following argument(s)” because you can pass in more than one. You can chain a series of callables, allowing you to do things like authentication, or header rewriting, …

There’s a lot that I’m not showing you but it’s fairly easy to learn once you got it up and running. So go learn it!

(I assume you know the client-side way of calling these functions, if not, browse around this blog or the internet for great tutorials on how to make REST calls using ajax, angular, or one of a million other methods)

Tagged , , ,