Welcome Guest, Not a member yet? Register   Sign In
Working on a REST API, critique requested
#13

[eluser]Yorick Peterse[/eluser]
Right now the very basic idea of the REST idea is working, the code looks as following:

Controller (ignore the debug function, I'm using it to test the API)

Code:
<?php
/**
* @name     Api controller
* @version     1.0
* @author     Yorick Peterse
*
* This is the main API controller, it will handle the incoming requests and will decide what to do with them.
* Do note that all functions in this controller which aren't supposed to be accessed through the URL, should be declared as private. This to prevent security issues
*
*/
class Api extends Rest_Controller {
    // Variables
    private $format;
    private $method;
    
    // Constructor function
    function __construct() {
    // Load the parent constructor
    parent::Controller();

    // Set the format, this will be set to XML by default
    $this->format = strpos($_SERVER['HTTP_ACCEPT'],'xml') ? 'xml' : 'json';
    // Set the method, by default this will be 'get'
    $this->method = $_SERVER['REQUEST_METHOD'];
    
    }
    /**
     * Main REST functions
     *
     * By adding these functions to the URL, Flork will determine what has to be executed.
     */
    
    // Index function, this function will redirect to the systems function
    function index() {
    // Load the URL helper
    $this->load->helper('url');
    redirect('Api/system');
    }
    
    // Function that will handle incoming data
    function system() {
    // Load the model
    $this->load->model('Model_api');
    // Get the table from the URL
    $table     = $this->uri->segment(3);
    $field    = $this->uri->segment(4);
    $value  = $this->uri->segment(5);
    
    // Do a switch statement to see what the request method is
    switch($this->method) {
        // Get
        case 'GET':
        $this->model_api->getData($table,$field,$value,$this->format);
        break;
        // Post
        case 'POST':
        
        break;
        // Put
        case 'PUT':
        
        break;
        // Delete
        case 'DELETE':
        
        break;
    }
    }
    
    // DEBUG FUNCTION, ONLY TO BE USED TO DEBUG THE INDEX FUNCTION BY SENDING REQUESTS
    function debug_request() {
    // Initialize Curl
    $curl = curl_init();
    
    // Set the options for Curl
    curl_setopt($curl,CURLOPT_URL,'http://localhost/Big_Projects/Flork/index.php/Api/system/users');
    curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'GET');
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    
    // Store the results in the $data variable and echo them
    $data = curl_exec($curl);
    curl_close($curl);
    
    echo $data;
    }

    
}
?>

The Model

Code:
<?php
/*
* @name Model_API
* @version 1.0
* @author Yorick Peterse
*
* The model for the API, what it does goes without saying.
*
*/
class Model_api extends Model {
    
    // Constructor function
    function __construct() {
    parent::Model();
    }
    // Function to output data in XML or JSON
    function getData($table,$field,$value,$format) {
    // Create the query
    if($field != null AND $value != null) {
        $query = $this->db->get_where("$table",array($field => $value));
    }
    else {
        $query = $this->db->get($table);
    }
    
    // Convert the array to the format
    $results = $query->result();
    
    print_r($results);

    }
    
    // Function to store the data that was sent using an HTTP request.
    function postData($table,$data) {
    
    }
    
    // Function to update existing data
    function putData($table,$id,$data) {
    
    }
    
    // Function to delete data
    function deleteData($table,$id) {
    
    }
    
    /**
     * Converting data
     *
     * 2 main functions, getXML and getJSON. These functions will convert an array to either XML or JSON
     */
    
    // Function to convert an array to XML format
    private function getXML($array) {
    // First check if the data is actually an array
    if(is_array($array)) {
        
    }
    // Return false, in case the $array variable isn't an array
    else {
        return false;
    }
    }
    
    // Function to convert an array to JSON format
    private function getJSON($array) {
    
    }
}
?>


Messages In This Thread
Working on a REST API, critique requested - by El Forum - 05-29-2009, 08:13 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 09:15 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 09:25 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 09:30 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 09:35 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 09:37 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 09:39 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 09:43 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 09:48 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 09:55 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 10:03 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 10:31 AM
Working on a REST API, critique requested - by El Forum - 05-29-2009, 12:45 PM
Working on a REST API, critique requested - by El Forum - 06-03-2009, 03:53 AM
Working on a REST API, critique requested - by El Forum - 06-03-2009, 05:10 AM
Working on a REST API, critique requested - by El Forum - 06-03-2009, 05:29 AM
Working on a REST API, critique requested - by El Forum - 06-03-2009, 08:27 AM
Working on a REST API, critique requested - by El Forum - 06-03-2009, 08:28 AM
Working on a REST API, critique requested - by El Forum - 06-03-2009, 09:21 AM
Working on a REST API, critique requested - by El Forum - 06-03-2009, 09:26 AM
Working on a REST API, critique requested - by El Forum - 06-03-2009, 09:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB