[STARTER] Slim framework complete example

In this short tutorial, I will show how to create a complete Slim framework starter application with the use of database and a service layer.
The code can be found here:

1. Installation (http://docs.slimframework.com/)

Install composer in your project:

curl -s https://getcomposer.org/installer | php

Create a composer.json file in your project root:

    "require": {
        "slim/slim": "2.*"

Install via composer:

php composer.phar install

This will be your application’s index.php file:


require 'vendor/autoload.php';
require 'services/CategoryService.php';

$catService = new \Service\CategoryService();
$app = new \Slim\Slim();
$app->response->headers->set('Content-Type', 'application/json');
$app->get('/category/:name', 'getCategory');


function getCategory($name) {
    global $catService;
    $namez = $catService->getCategory($name);
    echo json_encode($namez);

CategoryService is a php class that resides under the services folder:

namespace Service;
require 'services/DatabaseService.php';

class CategoryService {
    private $dbService = null;
    public function __construct() {
        $this->dbService = new \Service\DatabaseService();

    public function getCategory($name) {
        $sql = "select * FROM category where name = '$name'";
        $results = $this->dbService->executeQuery($sql);
        return $results;

And finally DatabaseService under the same services folder (change the DB connection settings):


namespace Service;

use \PDO;

class DatabaseService {

    private static $connection = null;

    private static function initConnection() {
        $dbhost = "localhost";
        $dbuser = "yourusername";
        $dbpass = "yourpassword";
        $dbname = "testing";
        $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        self::$connection = $dbh;

    public static function getConnection() {
        if (self::$connection == null) {
        return self::$connection;

    public function executeQuery($sql) {
        try {
            $db = self::getConnection();
            $stmt = $db->query($sql);
            $results = $stmt->fetchAll(PDO::FETCH_OBJ);
            $db = null;
            return $results;
        } catch (PDOException $e) {
            echo '{"error":{"text":' . $e->getMessage() . '}}';


A request to:
will show all your categories.

[SOLVED] Slim framework cannot make routing work

In Slim framework, cannot make routing work well as in the example provided on the site:

$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";

Only / will work. All the other URLs like /hello/Bob will throw a 404.
Solution is to setup apache correctly.
1. In the same folder with your index.php create a .htaccess file:

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

2. In the httpd.conf of apache server put this at the end. Change as needed – mysite.com should be replaced with something like slim.localhost, also documentRoot and Directory should point to the correct path of apache:

<VirtualHost *:80>
    ServerAdmin me@mysite.com
    DocumentRoot "/path/www.mysite.com/public_html"
    ServerName mysite.com
    ServerAlias www.mysite.com

    #ErrorLog "logs/mysite.com-error.log"
    #CustomLog "logs/mysite.com-access.log" combined

    <Directory "/path/www.mysite.com/public_html">
        AllowOverride All
        Order allow,deny
        Allow from all

3. Restart apache