Developed a Vegan API with Slim Framework

■ SLIM Introduction

In the process of examining which module is better for creating API in PHP,
I learned the existence of PHP micro-framework “Slim”.Slim is becoming the opted framework for API development and small web apps. Although you can create REST API in other frameworks such as  CakePHP or ZendFramework, you will need time and effort to learn to use and might not be suitable for rapid development scenarios. So this time for me, to create a vegan/vegetarian API service in a short span of time, SLIM was the best choice for the task. Because I didn’t want to spend a lot of time on other full-stack frameworks! Well, I could have used Cakephp2 but still, for an API service, I would still prefer microframeworks.

■ Here is a sample of my vegan API made by the SLIM framework.

 http://anteprocess.versus.jp/api/veganapi/shoplist/vegan?apikey=

To get the list of vegetarian shop lists, replace /vegan with /vegetarian. You will also have to set the API key to retrieve the data.

After setting the API key and run the request, you will get the data in JSON format like the following.

vegan shoplist sample

You can use this API for mobile/web applications, and it is totally free, but contact me for the API key! I will be adding more features to this service soon!


■ SLIM is a Microframework, right? Wait, what is a microframework?

Microframework is used to refer to minimalistic web app frameworks. Although microframework has only a minimum function, it is a framework with low cost to install and use.Besides Slim, there are Silex and Limonade.

The reverse of the microframework is called full stack.
Full stack has a high learning cost, but it is a framework that has all the necessary functions in one.There are Laravel and CakePHP and others.


■ How to install and use?

It can be simply installed with a composer.
The installation will be done below with reference to the official website tutorial.

php composer.phar create-project slim/slim-skeleton app_name

Once the application name directory is created,
Set the application name/public as the document root and access it from the browser.

■ How do you use it? Any sample?

First one specific example
get () method

$app = new \Slim\Slim();
$app->get('/test',function($request, $response, $args){
  echo "test";
});
$app->run();

■ When you write it easily it will be like the following
The get () method takes two arguments.
First argument: route pattern ‘/ test’
Second argument: root callback function () {echo “test”}

★In this case, echo “test” will be given if there is a request with get in / test.

■ What to do in case of a request other than get ()?

If you want to use post(), change $app-> get () to post, or if you want to use delete all you have to do is to replace the get() to delete().
Use any () to add a processing route to all HTTP request methods.

$app->any('/test', function($request, $response, $args){
  echo "test"
});

This makes echo “test” when there is an HTTP request such as get or post in /test.

■ Learn more about route patterns
The part described here is the part of ‘/test’ of $ app-> get (‘test’, function () {});

PlaceHolder

$app->any('/search/{id}', function($request, $response, $args){

For example, when displaying the search result, you can use it when /search is followed by the search target id.
The value of {id} has a value of $args of the third argument.

About option
The option is used when you want to behave the same regardless of whether it is present or not.
For example ‘/test’ and ‘/test/’ will be enclosed in brackets [] in order to make it the same even if there is neither / at the end.

■ A brief example of root callback
The part described here is part of function () {} of $ app-> get (‘test’, function () {});

There are three Callback arguments

function($request, $response, $args){}

First argument: Psr \ Http \ Message \ ServerRequestInterface Object representing the current HTTP request
Second argument: Psr \ Http \ Message \ ResponseInterface An object representing the current HTTP response
Third argument: An associative array containing the value of the placeholder for the name of the current route

$app->any('/search/{id}', function($request, $response, $args){ // For example if the {id} value is 100
    echo $args['id']; // 100
});

Ok, I introduced only the basics of slim, so if you want to check out more information about this awesome framework, go check out the official website!

https://www.slimframework.com/