Slim is micro framework used to work with REST Web Service. REST stands for Representational State Transfer which uses HTTP requests to GET, POST, PUT and DELETE data. Slim makes easy to manage server side codebase where all the server side data stored at single place and can be used by IOS App, Android App and Website as well.

This article explains you how to create REST API using slim framework. Here, I will create sample API to login in application and perform the operation that need user permissions.

Rest Web Service API in Slim-sgeek

RESTful Web Service API in Slim

Let’s start creating service through Slim. It arranges your code into proper format and reduces code complexity.

Directory Structure

We need two folders vendor, classes which contains user classes. vendor folder will generate automatically when you update composer.

Install Composer

You need composer in your system in order to install slim framework.

Read More:

Learn How to Install Composer

Install Slim Framework

Open command prompt or Terminal and target to Project Directory. Now, Install slim by executing below command.

Composer will install slim and creates compser.json file in project root directory.

Directory Structure

Create appropriate database tables by executing below queries or download and install database structure file.

Create Database


Stores details of users.

Note: The tables are only for sample, you can add new fields as per requirement.



Stores employee’s leaves data.

Getting Started

Require the Composer autoloader into your PHP script, and you are ready to start using Slim. Include db.php will use for various database operations.


Copy and past below code in .htaccess file.


Holds all the database related functions such as Login user, Logout User, Getting list of Leaves and Authorize User.

login($username, $password) used to check weather user is exists in database table or not.
verifyToken($token) validates token generated at the time of login against user’s record stored in database.
getLeaves($token) used to fetch user’s leaves.
logout($token) used to logout user.

You can use NotORM as well provided by slim. We have create new connection of PDO.

Chrome Extension

Install chrome extension from chrome store to test API response. I recommend to install Postman REST Client Extension. href=“ target=_blank”>Postman REST Client Extension

Login User

Create new Slim App and create route for login URL.

URL: http://localhost:80/slim/login

Function will get $username and $password from user via $_POST method and checks user is either exists in database or not. If User is exists in table new token will be generated and returns in JSON response. Store $token value in global variable will be used later while getting user leaves.


Login Response Rest Api Slim-Sgeek

Getting User’s Leaves

Here, will request get list of leaves if user token verified successfully.

URL: http://localhost:80/slim/get/leaves

First of all user will be verified based on token you have passed in request. If user found having same token than leave records for that user will be returned in JSON format.


Get Leaves Response Rest Api Slim

Logout User

Logout the user by removing his current token. If Token is not exists than request will return 401 error code with error message Eg. "Invalid Token".


Logout Response Rest Api Slim

index.php (Full Code)

Here, Single user can logged in from only one device simultaneously. If you has already logged from one device and tries to login from another than he will logout from first device automatically.

Read More:

Create Twitter Bot Using Python and Tweepy

sSwitch – JQuery Plugin For Sliding Toggle Switches

How To Create Simple JQuery plugin

How To Implement Infinite Scroll Using JQuery, Ajax and PHP

Integrate Google Prettify in Html Page

Verify Your Server Meets PayPal Payment Gateway Requirements

Integrate google reCAPTCHA in your website

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">