How to build an API with Slim PHP and RedBean ORM.

Ilerioluwa David
3 min readMay 12, 2020

--

Slim Micro Framework

API stands for Application Programming Interface.

An application programming interface is a computing interface which defines interactions between multiple software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc.

SLIM PHP

Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs.

Under SLIM PHP At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate callback routine, and returns an HTTP response.

Image Source: Find Nerd

RED BEAN ORM

First of all, what is ORM?

ORM simply means Object Relational Mapper.

Object-relational mapping in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language.

Note: When you use ORMs you don’t need to write traditional SQL codes to interact with your javascript.

red bean PHP: s an independent, free, BSD licensed, open-source object-relational mapping software written by Gabor de Mooij. It is a stand-alone library, not part of any framework. RedBeanPHP is an on-the-fly object-relational mapper, this means there is no upfront configuration.

Installation Process

Visit SLIM Official Website

After a successful installation, Create these folders in your app directory “app”, “bootstrap”, “public”, “resources”, “routes” folder.

Because we are using an MVC Structure to handle this approach.

Folder Structure

  1. bootstrap-> App.php
  2. app->Controllers, Models
  3. public->css, js , .htaccess, index.php
  4. resources->views->index.twig
  5. routes->web.php

The web.php file contains the file that points out our routes it their specific URLs.

If you are new to the slim framework you can visit their official website for better documentation.

Application Structure

Under Our Controller Folder, we create two files called ApiController.php and Controller.php

We will use the ps7 autoloader to load in our namespaces.

.hatccess file to manage our URLs

.htaccess file

App.php file under Bootstrap Folder

App.php

web.php handles our routes

web.php

Controller.php: We use this to get slim php active container.

Controller.php

ApiController.php Controllers controllers and add functionality to our routes request.

ApiController.php

So now that the ApiController has been done by adding its functionality

With these API we can get all users, update all users, delete a user and add a user.

These API can now be accessible with any programming language.

So if I point out to these URLs I can access my APIs.

  1. http://localhost/Api/public/getAll
  2. http://localhost/Api/public/getAll/1
  3. http://localhost/Api/public/getAll/add/1
  4. http://localhost/Api/public/getAll/delete/1
  5. http://localhost/Api/public/getAll/update/1

So it has been a wonderful write-up. Thank you for reading

--

--