PHP - How To Create A Rest Service With The Limonade Framework

In a nutshell, Limonade is a PHP framework that is designed for rapid development and prototyping. An easy-to-master framework such as Limonade provides all the features you'd expect in a modern framework. Developers should learn to use an appropriate framework in order to develop rapid applications effectively since they save time and effort

In this guide, you will create a rest web service using the Limonade framework and deploy it to a local webserver.

When you're finished, you'll be able to include the framework as a composer dependency, create the rest service and deploy it to a local webserver.

Prerequisites

Step 1 — Create The Project Directory And Include Limonade As A Dependency 

Let's start by creating a working directory:


mkdir /home/digitalriver/phpprojects/helloworld_im_using_limonade

Navigate to the working directory:


cd /home/digitalriver/phpprojects/helloworld_im_using_limonade

Create the composer.json file:


touch composer.json

Edit the composer.json file and add the following contents to it:

{

  "require": {

     "sofadesign/limonade": "@dev"

  }


}

Save and exit the file...

Run the following command to install the Limonade framework:


composer install

You'll see the following output:


Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking sofadesign/limonade (dev-master 097f28d)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing sofadesign/limonade (dev-master 097f28d): Extracting archive
Generating autoload files

Step 2 — Create The Limonade Rest Service

Let's create the index.php file that we will use to create the rest service by running the following command:


touch index.php

Modify the contents of the class file using your favorite text editor for this example we are using vi:


vi index.php

Once the text file opens up in the terminal window, type out the following:

<?php

header('Content-type: application/json');

require __DIR__ . '/vendor/autoload.php';

# Defining our route
# RESTFul mapping:
# HTTP Method | Url path | Controller function
# ------------+------------------------+---------------------
# GET | / | hello_im_using_limonade

# matches GET /hellolimusinglimonade
dispatch('/', 'hello_im_using_limonade');
function hello_im_using_limonade(){
return "{'hellogreeting': 'Hello Im Using Limonade!'}";
}

# Run the limonade app
run();
?>

Save the file contents and quit the text editor.

Step 3 — Copy The Project To A Local Webserver

Next, we are going to copy our project to our local webserver. In this case, we are just going to use the /var/www/html/ directory. To copy the project over; run the following command with root or a user that has permissions to the webserver directory:


sudo cp -rfv ./helloworld_im_using_limonade /var/www/html/

Type in the required password when prompted.

Step 4 —Verify that the rest service is deployed

You can verify that the rest service is deployed by using curl or your favorite browser. 

To test using curl run the following command in your terminal:


curl -I http://localhost/helloworld_im_using_limonade/index.php

You should get output similar to the following:


HTTP/1.1 200 OK
Date: Thu, 10 Feb 2022 05:06:18 GMT
Server: Apache/2.4.41 (Ubuntu)
X-Limonade: Un grand cru qui sait se faire attendre
Set-Cookie: LIMONADE0x5x0=g0tiut4b8ra4r8078dt5mvi5kn; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Type: application/json

To test using your browser you can navigate to the following url:

http://localhost/helloworld_im_using_limonade/index.php

Conclusion

In this tutorial, you have downloaded the Limonade framework, created a simple rest web service and deployed it to your local webserver. We build programs on top of a programming framework so that programmers can increase their coding efficiency. Frameworks allow programmers to write more efficient code. This allows them to develop better applications in less time.

Comments