This Rest API tutorial help to create REST API using PHP Restful API Framework SLIM for any android, IOS, Angularjs etc application.

Earlier, I have shared Rest API Using Lumen tutorial. The SLIM is another popular micro PHP framework to develop a restful API or web applications.

This is the first part of the slim rest api tutorial example. I’ll create configuration and database connection in this tutorial, rest of the work I will share in the second part of this tutorial.

In Slim Rest Example Part II, I will create a REST API that can create GET, POST, PUT and DELETE type requests.

Updated :

How to Create a Simple REST API Using Slim Framework -Part II

What is PHP Slim Framework

PHP Slim framework is a PHP micro framework that helps you quickly write simple and powerful web applications and APIs.Slim supports HTTP router, middleware, PSR-7 HTTP message, Dependency Injection, and much more features. The latest version of the slim framework is 3.8.1.

Read: Official Docs

How to configure Slim framework on XAMPP/WAMP

We will use a slim skeleton to create the Rest API, So we need to download Slim framework and unzip this. Let’s rename Slim-Skeleton-master folder to slim_test and paste into XAMPP/htdocs or wamp/www folder.

Now project folder structure will look like the below,


We will install dependency modules using composer if you can get more information about the composer from Here.
Now cd to wamp/www/slim_test and run composer install command.

D://wamp/www/slim_test> composer install

A composer install command downloads all dependencies modules and stores them into /vendor folder.

How to connect MySQL with Slim Framework

I am using the MYSQL database for this slim rest api tutorial. I will create a sample database whose name is customer_db . I will create the DB connection method in index.php file, Now I will edit slim_test/public/index.php file and add below code in the end of the file.

function getConnection() {
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
return $dbh;

The MySQL Database parameters are,

dbhost: This will contain the IP address or hostname of the MySQL server.
dbname: The name of the database.
dbuser: The username of the MySQL database.
dbpass: The password of the MySQL database.

Now open http://localhost/slim_test/public , You will find a slim home page otherwise throw an error message.

We will run the below SQL query into customer_db database to create a customer table,

-- Database: `test`
-- --------------------------------------------------------
-- Table structure for table `customer`
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`address` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`country` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`phone` int(11) NOT NULL,
`created_by` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`updated_by` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,

How to Configure CORS in Slim

This is a very important functionality in any rest API application, if you haven’t enabled CORS then probably you will get 500 error at the time of call rest API. We need to add a module corsslim into the slim framework.

Since we are using composer so I’ll add "palanik/corsslim": "dev-slim3" into the composer file and run again 'composer update' command. We will do some CORS configuration into public\index.php file.

$corsOptions = array(
"origin" => "*",
"exposeHeaders" => array("Content-Type", "X-Requested-With", "X-authentication", "X-client"),
"allowMethods" => array('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS')
$cors = new \CorsSlim\CorsSlim($corsOptions);

Genislab Technologies

NexGeneration complete end-2-end software testing & modern development operations tooling & solutions

Do you want to discuss your testing requirements with us? please don’t hesitate to hit the contact us button below, and we will get back to you at our earliest..