Build Simple REST API with Slim Framework

REST stands for Representational State Transfer. RESTful Web services are one way of providing interoperability between computer systems on the Internet. Rest API help to communicate between the client app and the server application.REST is an architecture style for designing networked applications. A REST API defines a bunch of functions which developers can perform requests and receive responses via HTTP protocol such as GET and POST.
In this tutorial, you will learn to how create CRUD (create, read, update, delete) operation REST API with Slim Framework. This is a very simple example, you can just copy paste, and change according to your requirement.
Before started to implement the REST API with Slim Framework, look files structure:
  • build-simple-rest-api-with-slim-framework
    • logs
    • public
      • index.php
      • .htaccess
    • src
      • config
        • config.php
      • libs
        • auth.php
        • DBConnection.php
        • mail.php
      • models
        • Employee.php
      • routes
        • api.php
    • vendor
    • .env
Understanding REST API
REST provides a block of HTTP methods which are used to alter the data. The following are common HTTP methods:
  • GET — is used for reading and retrieving data.
  • POST — is used for inserting data.
  • PUT/PATCH — is used for updating data.
  • DELETE — is used for deleting data.
Step 1: Create MySQL Database and Table
For this tutorial, you need a MySQL database with the following table:
Step 2: Create file named .env
loads environment variables
Step 3: Connect to Database file named DBConnection.php inside “src/libs/” folder.
The code below shows the database credentials
Step 4: Create class
Create a class file named Employee.php inside “src/models/” folder.
  • The Employee class handles the CRUD process
  • __construct() — Loads the required DBConnection.
  • empAdd() — Add Employee Record.
  • empUpdate — Update Employee Record.
  • empList() — get Employee all Records.
  • empDetails() — get Employee single Record.
  • empDelete() — delete Employee Record.
Step 5: Add Employee Record — POST Method
Create a route and sends a post request to /employee/add with required data, the app will add a new record to the database.
We will check for POST HTTP request and call method empAdd() to insert emp data to MySQL database table and return JSON data
In this method empAdd() from class Employee.php, we will insert record into emp table.


Step 6: Read Employee Record from the Database — GET Method
Create a route /employee and /employee/{emp_id} to get emp records to MySQL database. We will check for GET HTTP request and call method empDetails() or empList() to get emp data to MySQL database table and return JSON response
In this method empDetails() or empList() from class Employee.php inside “src/models”, we will get record(s) into emp table.
Get All emp Records


Get emp single Record


Step 8: Update Employee Record — POST Method
Create a route /employee/update to update emp records to MySQL database. We will check for POST HTTP request and call method empUpdate() to update emp data to MySQL database table and return JSON response
In this method empUpdate() from class Employee.php inside “src/models”, we will update record into emp table.


Step 8: Delete Employee Record from the Database — GET Method
Create a route /employee/delete/{emp_id} to delete emp record to MySQL database. We will check for GET HTTP request and call method empDelete() to delete emp data to MySQL database table and return JSON response
In this method empDelete() from class Employee.php inside “src/models”, we will delete record into emp table.


Step 8: Here is the the complete final src/routes/api.php file