slim Getting started with slim

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


This section provides an overview of what slim is, and why a developer might want to use it.

It should also mention any large subjects within slim, and link out to the related topics. Since the Documentation for slim is new, you may need to create initial versions of those related topics.


VersionDescriptionRelease date
3.8.1This content related to slim version 32017-03-19

Get Json Data From Database table (REST API)

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';

$app = new \Slim\App;

$app->get('/employee/view', function ($req, $res) {

    $con = new mysqli('localhost','USERNAME','PASSWORD','DATABASE');

    $query = $con->query("SELECT * FROM employee"); 
    while ($row = $query->fetch_assoc()) {
        $data[] = $row;
    return $res->withJson($data); 


Getting Started With Slim Framework

Installation or Setup Slim framework

  • Install Composer
  • Open cmd
  • Go to Root directory of your Project Folder and Run Following Command.

composer require slim/slim "^3.0"

Now you will have vendor directory in your project

Next Create Index.php in root folder and add following code

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';

$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");

    return $response;

Then Run Project on Localhost and try with following command



Hello any-thing

Hello World Example


include "vendor/autoload.php";

$app = new \Slim\App();

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


Installation or Setup

Recommended way to install Slim framework is by using composer.

  1. Create an empty directory

This directory will contain all required files for our Slim application to run. We call this directory root directory so we can address all other application files and directories relative to root directory.

mkdir slim-app
  1. Install Slim framework and its requirements using composer
cd slim-app
composer require slim/slim "^3.0"

From now on, we assume this is our working directory.

After composer finishes downloading required files, there should be two files composer.json and composer.lock and a directory named vendor which includes files downloaded by composer. Now we're ready to create our application. To organize our application, we create another directory:

mkdir public

We call this the public directory and we're going to tell our web server to server our application from this directory.

  1. Sample "Hello World" application

To use Slim create an index.php in public directory with following code:



include "../vendor/autoload.php";

$app = new \Slim\App();

$app->get('/', function ($request, $response, $args) {
    $response->getBody()->write("Hello world!");

  1. Start PHP built in server

We can now use PHP built in server to serve our application:

php -S localhost:8080 -t public

and run project by opening this address in a web browser:



Hello world!

Now configure the webserver so that all requests are passed to this file:

Apache configuration for clean URLs (Optional)

This is not required but recommended for slim projects to remove index.php in API URL.

Create .htaccess in the same folder where your index.php is located. The file should contain the following code:

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

Make sure your Apache virtual host is configured with the AllowOverride option so that the .htaccess declared rewrite rules can actually be used:

AllowOverride All

Ngnix configuration


Scaffold skeleton project with `composer` and @akrabats `Slim-Skeleton`

  1. Depending on whether you have installed composer globally or locally.

    • Locally: php composer.phar create-project slim/slim-skeleton {my-app-name}
    • Globally: composer create-project slim/slim-skeleton {my-app-name}
  2. If you are running a webserver (ex. Apache or nginx) point your virtualhost document root to your new applications public folder ex. {my-app-name}/public .

  3. Ensure logs/ is writeable by your webserver setup.

You can also run the whole shebang in dev mode with:

  • Locally: php composer.phar start
  • Globally: composer start

Credit: Rob Allen @akrabat and the SlimPHP team.

PS: I take absolutely NO credit for this. Almost verbatim from slimphp/Slim-Skeleton

Got any slim Question?