Laravel Micro Rest Framework – Simple Example of RESTful API in Lumen

Lumen is very popular,fast and light weight rest api micro-framework by Laravel using php. This tutorial help to access third party API using lumen rest framework.I am using sample third party rest service.I will call these rest call from lumen framework.The rest call can be Gitlab,jenkins,F5 and CF etc.You can access third party rest call from lumen framework easily.

You just need to create rest route and then action method in controller file to access thease services.The structure of code is like below,

  • app/Typicodemgmt : This folder will contain all Typicode services wrapper method and interface
  • app/Http/Controllers/ : This folder will contain controller file and action method to access rest call

You can also check other recommended tutorials of Lumen/Laravel,

Step to access third api Rest call Using Lumen

Step 1: We will create Typicodemgmt folder into app/folder.
Step 2: We will create Typicode service folder in app/Typicodemgmt/Service/ here, the folder structure look like app/Typicodemgmt/Service/Typicode.

Step 3: We will create new typicode service file in this location Typicodemgmt\Service\Typicode\Laravel\TypicodeService.php. We will add below code into this file,

namespace Typicodemgmt\Service\Typicode\Laravel;

use GuzzleHttp\Client;
use Typicodemgmt\Service\Typicode\TypicodeInterface;

class TypicodeService implements TypicodeInterface {
}

Step 4: We will create new interface in location of Typicodemgmt\Service\Typicode\TypicodeInterface.php. We will add below code into this file,


This file contains all interface methods for your Typicode module if you need,

Step 5: We will create new service provider call file which is using above interface class in location of Typicodemgmt\Service\Typicode\TypicodeServiceProvider.php. We will add below code into this file,

app;
	  
      $app->bind('Typicodemgmt\Service\Typicode\TypicodeInterface', function($app) {
         return new TypicodeService();
      });
   }
   
}

Step 6: Now we will create controller file in location of app\Http\Controllers\TypicodeController.php. We will add below code into this file,


step 7: We will create constant which will host url of rest call.We will create _client() methods which which return Guzzle Client instance of Rest service in Typicodemgmt\Service\Typicode\Laravel\TypicodeService.php.

var $typicodeAPI = "http://jsonplaceholder.typicode.com/";
   private function _client($endpoint) {
      $client = new Client([
          'base_uri' => $endpoint,
          'timeout' => 300,
		  'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json"],
          'http_errors' => false,
          'verify' => false
      ]);
      return $client;
   }
 

After that, we have created basic structure for Typicode module to access third party rest call.We can use service class,provider class to access rest end points and passed response to controller method which is accessible publicly to end user.

How to access list rest call using Lumen

step 1: We will create route in route.php file,
$app->get('posts', 'TypicodeController@getAllPosts');

step 2: Create interface method in TypicodeInterface.php file.
public function getPosts();

step 3: Now define interface method in TypicodeService.php file. We will create getPosts() which will host url of rest call.

   /**
	 * Method to fetch all posts
	 *
	 * @author parvez.alam
	 */
   public function getPosts() {
		$client = $this->_client($this->typicodeAPI);
		
        $response = $client->get('posts');
		$data = $response->getBody()->getContents();
		
		return json_decode($data);
		
   }
   

step 4: Now access above service method from controller action method.We will write below code into TypicodeController.php file.

	protected $typicode;

	public function __construct(TypicodeInterface $typicode) {
      $this->typicode = $typicode;
	}
  /**
	 * Method to get all posts
	 *
	 * @author parvez.alam
	 */
   public function getAllPosts() {
	  $response = $this->typicode->getPosts();
	  if(!empty($response))
	  return response()->json(array("status" => "success", "results" => $response));
	  else
	  return response()->json(array("status" => "error", "message" => $response));
   }
   }
   

Conclusion :

We have created service wrapper method and access into controller method.This tutorial help to access third party api using lumen api rest framework.You can extend this code and add more rest method like get single post and delete post.

  • vijay

    great article!