In PHP restful API framework : Slim Tutorial, we will introduce you to PHP slim framework.There are many frameworks in PHP,Some of them are really very popular due for some certain reasons.

Slime overtakes all of them while talking with respect to APIs.

What is Slim?

Slim is a micro framework for PHP that allows you to quickly build web applications and APIs.It is super lightweight with compared to other PHP frameworks ,such as codeIgniter and Symfony, which have a lot of extra fluff.

This means that’s it really fast, and great for building a REST API for use on a multitude of devices.

What is Rest API?

A REST API acts as a middle-man between clients (mobile devices, computers) and the database server. This type of infrastructure allows clients using completely different technologies or operating systems to communicate in one type of protocol. That way as technologies change, the method of communication with the database and server logic can remain the same. Just remember, the application we are building is meant to be used with client applications and not really meant for actual users to be using it… so it’s not going to look very pretty. We will be building a nice looking client application later that will be used to interact with the API–and that will look pretty.

1.Install Slim to your Machine

First of all, open a terminal and write composer,To check the composer was installed in your machine already or not.

If you have done with composer then ,create a directory in your htdocs folder (i.e. slim),

Then open a terminal and navigate to this directory, and write following command.

It will install slim with all dependencies automatically at the given directory.

Now, you have done with Slim basic installation.

2.Create your first program.

To create file , we need to follow the following instruction

  • – Include autoload.php file.
  • – Instantiate a Slim application.
  • – Define the Slim application routes.

Create a file at the root of your directory.

In the following code, you can see two main parameters which are passed in get method.

  1. The route pattern (with optional named placeholders)
  2. The route callback

The route callback is a function having three parameters $request,$response and $args.

$request is for Server Request, if any such as any JSON or any POST data array.

$response is a response which will be given by method or output.

$args are the parameters which are passed in url.


Now go to the browser and write URL : http://localhost/slim/index.php or  http://localhost/slim/, where slim is your directory name.

It will display : Welcome: This is AlphansoTech Tutorial Guide.

Above code represents the default method of the index page, let us create another method.

we have just created another method, named friends. To call this method we need to write in browser,


Now,come to the point which is the core part of the slim tutorial. Why  should we choose slim framework especially working with Rest APIs.

  1. It’s very lightweight and focused on REST and nothing else.
  2. It supports all the HTTP methods (GETPOSTPUTDELETE), which was a key requirement for my application.

3.Create your first database call.

Slim doesn’t come with a database connection utility of its own. You can use any database connection toolkit you like.We’ll be using the MySQLi object, which is the modern way to connect to a MySQL database in native PHP.

Slim Tutorial provides an example of building a complete RESTful API using the different HTTPmethods:
  • GET for read or retrieve data.
  • POST for add or insert operation.
  • PUT for the update operation.
  • DELETE for destroying data.

Create a database and table

First of all ,create a directory (i.e. lib) and create a file for database connection


Now I have created a file on the root of the directory.

getParseBody() : Every HTTP request has a body.It can be JSON,XML or the post data array in our case.It is used for parse the HTTP request body into a native PHP format.


To Get Employees Go to browser and Write


O/P :


To get a certain Employ


O/P :

To check Insert,Update and Delete method, We use the RESTclient Add-One of Mozilla Firefox.You can also use the Advanced Rest Client of Google Chrome.

To Add data in the Employee table.

After installation ,Click on RESTclient Add-One in your browser and click on headers at navigation panel and choose custom Header.

It will display a pop window containing two placeholder name and value.

Set name = Content-Type and value = application/x-www-form-urlencoded.

Now, Make your call for adding data using RESTclient.

php restful api framework : Slim Tutorial

PHP restful API framework : Slim Tutorial

Same way, we can test update and delete method by changing URL ,as shown in above image.


Kindly,visit our blog regularly for learning more Slim Tutorial.
I hope you will find this post very useful regarding PHP restful API framework : Slim Tutorial. Let me know any questions if you have in comment regarding slim tutorial PHP restful API framework : Slim Tutorial.

Leave a Reply