A new week ends, and with that we present a new devlog introducing a new feature of BlizzCMS V2.
It’s something that we already had pending of BlizzCMS, and without more we are going to introduce you in the new API of BlizzCMS.
What is a REST API?
A REST API is nothing more than a library of functions that is accessed through HTTP, which in other words means access through URLs to obtain different resources through consultations or provide information.
The data returned can be XML, JSON, HTML among other formats which makes it most versatile for different applications to connect to it and thus create more complex applications or with a greater number of modules with different technologies, a simple way.
API IN BLIZZCMS
We are going to put some example cases so you can see how an API would work, suppose we want to formulate a query (for an external application) about the users that we have registered.
In a normal case, we would have to create a connection to the database, and with it the functions to show it.
In the case of the API, just create a function with the name of users_get.
function users_get ()
// reply with information about several users
With this we would already have a part done, now it would be necessary to fill it with information, which we can obtain from the Blizzcms models:
function users_get ()
$ users = $ this-> user_model-> get_all ();
if ($ users)
$ this-> response ($ users, 200);
$ this-> response (NULL, 404);
This URL looks like any other CodeIgniter URL with a controller and method, but you will notice in this diagram that the method is named a “Resource”. REST is about Resources and they are essentially a noun within your application, with which they are interacted (that is, added, deleted, edited, consulted) based on HTTP headers and request strings in the URL or HTTP arguments.
The default output format is XML, which is what we see in this basic example. The other links are a bit longer and show how to pass parameters and show how the output format can be modified in the URL:
Normally in CodeIgniter, you simply pass the parameter values, but a REST controller accepts any number of parameters in any order. For this to work, we need to pass the name of the parameter followed by the value in pairs.
At the end of the URL is the “format” parameter. This is a reserved parameter that will modify the output format of the requested data in the following way:
In general REST APIs are very useful when we want to interconnect applications of different technologies such as CodeIgniter with Android, IOS … in a supposed case of having a centralized management application with CodeIgniter and the Android application makes queries through the REST API, this would be one of the most common scenarios due to the great reach that mobile applications have today.