Skip to content

Commit

Permalink
Add: REST router for REST Full API
Browse files Browse the repository at this point in the history
  • Loading branch information
idoalit committed Jul 5, 2017
1 parent 94561db commit 96352bb
Show file tree
Hide file tree
Showing 8 changed files with 518 additions and 1 deletion.
31 changes: 31 additions & 0 deletions api/v1/controllers/BiblioController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* @author : Waris Agung Widodo
* @Date : 2017-07-05 12:15:12
* @Last Modified by : ido
* @Last Modified time : 2017-07-05 15:08:08
*
* Copyright (C) 2017 Waris Agung Widodo ([email protected])
*/

require_once 'Controller.php';

class BiblioController extends Controller
{
protected $sysconf;
protected $db;

function __construct($sysconf, $obj_db)
{
$this->sysconf = $sysconf;
$this->db = $obj_db;
}

public function detail($id, $token)
{
require_once __DIR__ . './../../../lib/api.inc.php';
$biblio = api::biblio_load($this->db, $id);
parent::withJson($biblio[0]);
}
}
21 changes: 21 additions & 0 deletions api/v1/controllers/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @author : Waris Agung Widodo
* @Date : 2017-07-05 12:17:02
* @Last Modified by : ido
* @Last Modified time : 2017-07-05 13:53:28
*
* Copyright (C) 2017 Waris Agung Widodo ([email protected])
*/

class Controller
{

public function withJson($array)
{
header('Content-type: application/json');
echo json_encode($array);
}

}
25 changes: 25 additions & 0 deletions api/v1/controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* @author : Waris Agung Widodo
* @Date : 2017-07-05 13:05:36
* @Last Modified by : ido
* @Last Modified time : 2017-07-05 13:12:51
*
* Copyright (C) 2017 Waris Agung Widodo ([email protected])
*/

require_once 'Controller.php';

class HomeController extends Controller
{

public function index()
{
$response = array(
'error' => false,
'message' => 'Sugeng rawuh is selamat datang.'
);
parent::withJson($response);
}
}
29 changes: 29 additions & 0 deletions api/v1/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @author : Waris Agung Widodo
* @Date : 2017-07-04 15:28:21
* @Last Modified by : ido
* @Last Modified time : 2017-07-05 15:04:29
*
* Copyright (C) 2017 Waris Agung Widodo ([email protected])
*/

/*---------- Require dependencies ----------*/
require 'lib/router.inc.php';
require __DIR__ . '/controllers/HomeController.php';
require __DIR__ . '/controllers/BiblioController.php';

/*---------- Create router object ----------*/
$router = new Router($sysconf, $dbs);
$router->setBasePath('api');

/*---------- Create routes ----------*/
$router->map('GET', '/', 'HomeController:index');
$router->map('GET', '/biblio/[i:id]/[a:token]', 'BiblioController:detail');

/*---------- Run matching route ----------*/
$router->run();

// doesn't need template
exit();
4 changes: 3 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@
echo $content_data['Content'];
unset($content_data);
} else {
header ("location:index.php");
// header ("location:index.php");
// check in api router
require 'api/v'.$sysconf['api']['version'].'/routes.php';
}
}
} else {
Expand Down
Loading

0 comments on commit 96352bb

Please sign in to comment.