In some of my last post I explain a various method to creating webservice using various technology.  Now this time I explain another PHP framework which is specially designed to create web services. If you want to create REST API for your mobile application in few minutes then Slim framework is really very helpful for you. To setup slim framework in your server, you can simply use composer. To create REST API in PHP using Slim follow bellow steps.

Step 1. Get a fresh copy of Slim framework using composer or you can manually download from their official website. If you are using composer and your composer install globally then run this command.

Step 2. After getting latest release of slim framework create index.php file include framework  library as shown bellow 

Step 3. Now initialize slim framework and create connection to your database as shown below

Step 4. Now if our database is connected successfully we can create a method to handle the request. You can handle bellow given type of request in Slim

  • GET
  • POST
  • PUT
  • HEAD

Now we create our first method you can create a method like this first you get your application $app and after request method like Get/Post as shown below.

Step 5.  Now if you want to get form data and perform some action on database. You can create a function like bellow. You can access your database connection on $this->db variable after this your can create your query. If you want to pass data to query you can use bindParam(key, value)  to pass value in your query.

Remember you need to initialize your app at the end to do so you need to add below code at the end of script

This is a very basic example. This post guides you to setup and makes a basic request to the server using Slim framework.  If you want to learn more about Slim framework please post a comment.

Sample code for creating Application in Slim


Also, read Secure your Slim web services using JWT Auth.

If you like my blog please like and share my facebook page and share my blog thanks.