How to build an API with Slim PHP and RedBean ORM.
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.
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
- bootstrap-> App.php
- app->Controllers, Models
- public->css, js , .htaccess, index.php
- resources->views->index.twig
- 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.
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
App.php file under Bootstrap Folder
web.php handles our routes
Controller.php: We use this to get slim php active container.
ApiController.php Controllers controllers and add functionality to our routes request.
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.
- http://localhost/Api/public/getAll
- http://localhost/Api/public/getAll/1
- http://localhost/Api/public/getAll/add/1
- http://localhost/Api/public/getAll/delete/1
- http://localhost/Api/public/getAll/update/1
So it has been a wonderful write-up. Thank you for reading