Now most developers are used in slim framework because its open source framework and flexible for develop the web application. Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

create restful api using slim framework

Slim framework supports PSR-7 which is the PHP standard for HTTP messaging, so you’ll notice as you build your application that the Request and Response objects are something you see often. This is a modern and excellent approach to writing web applications.

Create RESTful API using Slim framework

Start by making a folder for your project (mine is called project, because naming things is hard). I like to reserve the top level for things-that-are-not-code and then have a folder for source code, and a folder inside that which is my initial structure looks like this,

create restful api using slim framework

Installing Slim Framework

Composer is the best way to install Slim Framework. If you don’t have it already, you can follow the Installation instruction in my project I’ve just downloaded the composer.phar into my src/ directory and i’ll use it locally. So my first command looks like this (i’m in the src/ directory).

This does two things:
1. Add the Slim Framework dependency to composer.json (in my case it creates the file for me as I don’t already have one, it’s safe to run this if you do already have a composer.json file).
2. Run composer install so that those dependencies are actually available to use in your application.

To set up the git ignore correctly, create a file called src/.gitignore and add the following single line to the file:

Create the Application

There’s a really excellent and minimal example of an index.php for Slim Framework on the project homepage so we’ll use that as our starting point. Put the following code into src/public/index.php

Run the application in PHP Server

From the src/public directory run the command,

Database Configuration

The initial example uses all the Slim defaults, but we can easily add configuration to our application when we create it.

Open your src/settings.php file and configure your database setting by adding/editing below showing database config array.

Now open your src/dependencies.php file and configure database library. There are many database libraries available for PHP, but this example uses PDO – this is available in PHP as standard so it’s probably useful in every project, or you can use your own libraries by adapting the examples below.

In the below code we are injecting database object into container using dependicy injection, in this case called db.

Developing the API with Slim

Now that we have our Slim app up and running with database connection, we need to manage todos in the database.

This function simply return all todos information as you can see in this query, to call this API use this URL http://localhost:8080/todos.

Getting single todo – We are going to create a new route so that when a user hits /todo/{id}, it will return a todo in JSON format.

This function check record of given id and return if found anything, to call this API use this URL http://localhost/todo/1

Find todo by name – We are going to create a new route so that when a user hits /todos/search/{Query}, it will return a list of all matched todos in JSON format.

This function search in database for your given query, to call this API use this URL http://localhost/todos/search/bug

Add todo – We are going to create a new route so that when a user sends a post request to /todo with required data, app will add a new record to the database.

This API accept post request and insert submitted data in your database. To call this API use this URL http://localhost/todo

Delete Task – We are going to create a new route so that when a user sends a delete request to /todo/{id}, app will delete a record from the database.

Update Task – We are going to create a new route so that when a user sends a put request to /todo/{id} with required data, app will updated a record based on match parameter in the database.

This API accept put request and updates submitted data in your database. To call this API use this URL http://localhost/todo/{id}

Here is the the complete final src/routes.php file.

Click to download