Thursday, May 21, 2015

Basic RESTFul/json API

Web Development is constraint by frequent evolutions so that you need to rewrite the whole or part of a software every 5 to 10 years. The Dynamic Web Application was a server side MVC but it's clear today that we are also moving to client side Single Page Application MVC with libraries like AngularJS. Google may not be welcome in China but I need to follow latest technology innovation. I made a new Kernel with a very basic web application in Ajax using a RESTFul/json API written in PHP.

The Kernel analyses the request and exec the endpoint

 <?php  
 /**  
  * rest.class.php  
  * author : Fabrice Morisseau  
  * creation date : April 9, 2015  
  **/ 

 abstract class rest  
 {  
   /**  
    * Property: method  
    */  
   protected $method = '';  
   /**  
    * Property: endpoint  
    */  
   protected $endpoint = '';  
   /**  
    * Property: action  
    */  
   protected $action = '';  
   /**  
    * Property: config  
    */  
   protected $config = Array();  
   /**  
    * Constructor: __construct  
    */  
   public function __construct($request) {  
     header("Content-Type: application/json");  
     $this->config = explode('/', rtrim($request, '/'));  
     $this->endpoint = $this->config[0];  
     $this->action = $this->config[1];  
     $this->method = $_SERVER['REQUEST_METHOD'];  
   }  
   public function exec() {  
     if ((int)method_exists($this, $this->endpoint) > 0) {  
       header("HTTP/1.1 200 OK");  
       $data = $this->{$this->endpoint}($this->config);  
     } else {  
       header("HTTP/1.1 404 Not Found");  
       $data = "The endpoint doesn't exist";  
     }  
     return json_encode($data);  
   }  
 }  
 ?>  

Obviously the endpoint are stored in the API which call the Kernel


 <?php  
 /**  
  * MyAPI.php 
  * author : Fabrice Morisseau  
  * creation date : April 9, 2015  
  **/   
 require_once '../../kernel/rest.class.php';  
 class MyAPI extends rest  
 {  
   protected $request;  
   public function __construct($request) {  
     parent::__construct($request);  
     $this->request = $request;  
   }  
   /**  
    * Endpoint Document  
    * Todo : Eval phpfile for documentation  
    */  
   protected function document() {  
    /**
     * Example of endpoint return $content of the document
     **/   
   }  
 }
       
 try {  
   $API = new MyAPI($_REQUEST['request']);  
   echo $API->exec();  
 } catch (Exception $e) {  
   echo json_encode(Array('error' => $e->getMessage()));  
 }  
 ?>  


while the .htaccess route the requests to the API

 <IfModule mod_rewrite.c>  
 RewriteEngine On  
 RewriteCond %{REQUEST_FILENAME} !-f  
 RewriteCond %{REQUEST_FILENAME} !-d  
 RewriteRule api/v1/(.*)$ api/v1/MyAPI.php?request=$1 [QSA,NC,L]  
 </IfModule>  


We are done with latest technology innovations and my prototype has been totally rewritten.

The Object Relational Bridge is no longer used because modelization is not flexible so that you can realize complex things but details and specific queries also matter. Modelization looks like Artificial Intelligence and the Alchemist dream moreover according to this study by the French national center in scientific research CNRS there are numerous security issues with a dynamic system. It seems better to access your data on-demand with strict security checks.

Single Page Application loads only one page while the server generates all the html code with a dynamic web application. Among the important things to notice as mentioned on wikipedia SPA are Search Engine Optimization in the case of this RESTFul/json API and Browser History as well as navigation. Those issues are critical when designing your application. Your application should have an important core generated on the server and optimized for search engine as well as on demand requests accessible with this RESTFul/json API.