Create a RESTful Web Service API with Slim

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.

– 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.

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.

Response:

Getting User’s Leaves

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

Categories

Sgeek is how to blog maintained by Gopal Joshi. Gopal is Software Engineer and Blogger by passion and love to building web applications. Sgeek will share How To Technology Stuff, Tutorials of various technologies such as PHP, Symfony, Python, NodeJS, Frameworks and more.