Php Slim Framework Create REST api Tutorial

In previous tutorial, you already understand how to setup php slim framework in your computer and also learn how to change the domain name for your api. If you going to build a good mobile application or web application, you need the REST api to manage the database from the web server. It is a good practice for mobile app communicate with server and it is very simple to use and implement. In this tutorial, I will teach you basic method to use php slim framework create rest api for your mobile or web application.

HTTP Methods

There have 4 most used http methods that is very important when we are create the apis. Besides, there have other http methods which are Options, Head, Patch but it is less likely to use it. The good REST api should proper use the http methods. This tutorial I will teach you the following http methods which are GET,POST,PUT and DELETE.

GET To fetch a resource
POST To create a new resource
PUT To update existing resource
DELETE To delete a resource

HTTP Status Code

The Http Status Code will let the user know what error occur in the application. It is very important knowledge when create the REST api and use it. Sometime we will occur error, so we can figure out fast and fix it as soon as possible.

200 OK
201 Created
304 Not Modified
400 Bad Request
401 Unauthorized
403 Forbidden
404 Not Found
422 Unprocessable Entity
500 Internal Server Error

URL path

We need to use the proper url to specify the current feature so the user can refer to the url and know what the url will do for them. For an good example: GET http://abc.com/user/ – Will get all the user.

Simple REST API structure

The following table I list out url, method, parameters and the purpose of each api in this tutorial. This table will make you clearer on how to use it later.

URL HTTP Method Parameters Purpose
/user GET Get all user
/user/{id} GET Get specific user by id
/user/search/{query} GET query Search all user by name
/user POST  name,email,phone Insert a new user
/user/{id} PUT  name Update specific user by id
 /user/{id} DELETE Delete specific user by id

Create a new MySQL Database

Access your phpMyAdmin add a new database name “slim_db” after that insert the following SQL in query section. It will help you auto generate the table according the SQL.

File Structure

You can following my file structure in the following picture, or you can implement your own structure after this tutorial.

index.php – include all the API requests
.htaccess – Specify rules for url structure and apache

slim-rest-1

Create a config.php

This php file is to connect to the mysql database so you can do some change in your database.

Create a user.php

This php file to create the api and route each api to specific url so you can use it with different url.

/user (GET) – Get all user from your database and order by id. It will give you a json response so you can decode the json response and get data from your mobile app.

/user/{id} (GET) – Get specific user by id from your database, it only response for one user.

/user/search/{query} (GET) – Get the users according to the search query such as “abc”.

/user (POST) – Insert the new user in your database by using name, email and phone.

/user/{id} (DELETE) – Delete the user according to the id.

/user/{id} (PUT) – Update the user according to the id.

Edit index.php

 

How to Test?

You can go download REST api client to test the api is working or not. I suggest you to use Postman which are the best REST api currently in the market.

For example to get user

slim-rest-2

(Php Slim Framework Create REST api)

Source Code

 

(Visited 1,023 times, 1 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

Leave a Reply

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

GET YOUR EBOOK FOR FREE

Unlock free ebook "Beginning Programming ALL-IN-ONE Desk Reference for DUMMIES" now!

We will send a copy to your email