Tuesday, 21/11/2017 | 7:54 UTC+5
Mehta Websolution's Blog

SlimAPI Framework Installation

Post by relatedRelated post

Nowadays, The internet is full of third-party and custom APIs that offer a wide range of functionalities and Features. PHP offers several frameworks for web app, mobile app api development rapidly. However, time is always the crucial part of web developers and the app needs to be pushed at an impossible deadline. In such times, frameworks are the best option for rapid application development.

In this atricle, I will introduce you to Slim framework for PHP. Slim is fast becoming the most-opted framework for API development and small web apps as well as large web apps and mobile application development backend api. Although you can create REST API in several other frameworks like CakePHP, Symfony Laravel, Codeigniter, they have a steep learning curve and are often too cumbersome to use in rapid development scenarios.

Slim Frameword is microframe work which is only created for the developer who choose to develop apis.

Understanding Slim Framework

Slim is super lightweight framework, ideal for rapid web app development. One of the important usages is in REST API development. Slim supports all HTTP method (GET,POST,PUT,DELETE). Slim contains very handy URL structure with routers, middlewares, bodyparser along with page templates, flash messages, encrypted cookies and lots more.

At this point, it is important to understand the structure of the REST API.

Understanding REST API

REST is the abbreviation of Representational State Transfer. This is a bridge or medium between data resource and application interface, whether it’s on mobile devices or desktops. REST provides a block of HTTP methods which are used to alter the data. The following are common HTTP methods:

GET is used for reading and retrieving data.
POST is used for inserting data.
PUT is used for updating data.
DELETE is used for deleting data.

Basically, REST phenomena works on actions and resources. Whenever any action URL is invoked, it performs an individual method (or a set of methods) on that URL. I will further discuss this below with examples.

First we will need to install Slim framework for the REST API project.

System Requirements

  • Web server with URL rewriting
  • PHP 5.5 or newer

Role of Composer

  1. Enables you to declare the libraries you depend on.
  2. Finds out which versions of which packages can and need to be installed, and installs them (meaning it downloads them into your project).

If you are working on the localhost, you need to install it. If you haven’t installed it yet, just go to the following link and follow the instructions.

Instruction and more details about Composer Here.

How to Install Slim

We recommend you install Slim with Composer. Navigate into your project’s root directory and execute the bash command shown below. This command downloads the Slim Framework and its third-party dependencies into your project’s vendor/ directory.

After installing Slim, the following piece of code will require it in the index.php file to require autoload file and instantiate Slim.

Making a .htaccess File for Clean URL Structure

To make your life easier, you should create a .htaccess file that defines clean URL structure. At the root directory, make a .htaccess file and add the below code in it. This will provide a clean URL structure for the PHP file. (this just means that you don’t want to include PHP filename in the URL calls).

If your index file is located in different folder (for instance, the “public” folder), then you can insert the full path of the index file in the last line:

Create a Database in MySQL

Now it is time for the first API call. Let’s make it systematically.

Retrieving Data

Enter the following code in the index.php file to get all the books from the database. A GET call is used for retrieval.

To streamline working with the API calls, I recommend using Postman (available from the Chrome App Store). This plugin greatly helps in API management and usage.

In postman, make a GET call with API URL.

This is all for the basic REST API in the Slim Framework. However, this API will not work until you add this command at the end of the code.

Conclusion

Creating and using the REST API with Slim framework is very easy. The biggest advantage of the framework is its ease of use and lightweight. The icing on the cake is that it is very easy to learn and a good developer could pick up the framework in a matter of hour. To summarize, Slim receives HTTP requests, review them and invokes the appropriate callback routine for HTTP requests and return the appropriate response(s).

If you need clarification about this article or have any other query about the Slim Framework, do let me know through the comment section.

About

Mehta Websolution is a web development company based in Jamnagar, Gujarat, India. providing a full range of web services including web design, web development, web promotion, search engine optimization, web hosting and domain registration and all Internet services at accessible cost since it was established in 2007. A company which provides you a full range of web services Contact us for web design, web development, web promotion, search engine optimization, web hosting and domain registration at an affordable cost. IF you are looking for an IT Solutions Provider who can implement your Dreams in a reality form of exhaustive web applications, innovative software, and creative designs or for fastidious solutions and persistent services, you are at the right place. we provide you software development and web solutions all across the globe. Our strategic location and proven experience enables us to provide you with the best services and outsourcing for your Software Development, Web Development, E-commerce, Multimedia Presentations and Web Based Applications at the corporate level.

POST YOUR COMMENTS

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

four + nineteen =

Mehta Websolution Since 2007

Company is Government & Defence (Indian Navy, Army & Air Force) Approval I.T. Contractor, An ISO 9001:2008 Certified I.T. Company and Google Enterprise for Work Partner.