You've already forked ci_restful
first commit
This commit is contained in:
74
application/controllers/api/barang.php
Normal file
74
application/controllers/api/barang.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Example
|
||||
*
|
||||
* This is an example of a few basic user interaction methods you could use
|
||||
* all done with a hardcoded array.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @subpackage Rest Server
|
||||
* @category Controller
|
||||
* @author Phil Sturgeon
|
||||
* @link http://philsturgeon.co.uk/code/
|
||||
*/
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
require APPPATH.'/libraries/REST_Controller.php';
|
||||
class Barang extends REST_Controller
|
||||
{
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model(array('barang_model'));
|
||||
}
|
||||
function a_barang_get() {
|
||||
if(!$this->get('id')) { $this->response(NULL, 400);}
|
||||
else {
|
||||
$query = $this->barang_model->get_barang_by($this->get('id'));
|
||||
if($query) { $this->response($query, 200);}
|
||||
else { $this->response(array('error' => 'User could not be found'), 404); }
|
||||
}
|
||||
}
|
||||
|
||||
function all_barang_get() {
|
||||
$query = $this->barang_model->get_barang_all();
|
||||
if($query) { $this->response($query, 200);}
|
||||
else { $this->response(array('error' => 'User could not be found'), 404); }
|
||||
}
|
||||
function add_barang_post() {
|
||||
$data = array( 'id' => '',
|
||||
'nama' => $this->post('nama_barang'),
|
||||
'harga' => $this->post('harga'));
|
||||
|
||||
$quer = $this->barang_model->simpan_barang($data);
|
||||
|
||||
if($quer) {
|
||||
$this->response(array( 'succes' => 'yes'), 200);
|
||||
}
|
||||
else {
|
||||
$this->response($quer, 404);
|
||||
}
|
||||
}
|
||||
function update_barang_post(){
|
||||
$data = array( 'id' => '',
|
||||
'nama'=> $this->post->('nama_barang'),
|
||||
'harga'=> $this->post('harga'));
|
||||
}
|
||||
/*
|
||||
function update_barang_post() {
|
||||
$data = array( 'nama' => $this->post('nama'),
|
||||
'harga' => $this->post('harga'))
|
||||
$id = $this->get('id');
|
||||
$query = $this->barang_model->update_barang($data, $id);
|
||||
$this->response($query, 200);
|
||||
}
|
||||
|
||||
function del_barang_delete(){
|
||||
$id = $this->get('id');
|
||||
$query = $this->mbarang->del_barang($id); echo "";
|
||||
$this->response($query, 200);
|
||||
}
|
||||
*/
|
||||
}
|
95
application/controllers/api/example.php
Normal file
95
application/controllers/api/example.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Example
|
||||
*
|
||||
* This is an example of a few basic user interaction methods you could use
|
||||
* all done with a hardcoded array.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @subpackage Rest Server
|
||||
* @category Controller
|
||||
* @author Phil Sturgeon
|
||||
* @link http://philsturgeon.co.uk/code/
|
||||
*/
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
require APPPATH.'/libraries/REST_Controller.php';
|
||||
|
||||
class Example extends REST_Controller
|
||||
{
|
||||
function user_get()
|
||||
{
|
||||
if(!$this->get('id'))
|
||||
{
|
||||
$this->response(NULL, 400);
|
||||
}
|
||||
|
||||
// $user = $this->some_model->getSomething( $this->get('id') );
|
||||
$users = array(
|
||||
1 => array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com', 'fact' => 'Loves swimming'),
|
||||
2 => array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com', 'fact' => 'Has a huge face'),
|
||||
3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => 'Is a Scott!', array('hobbies' => array('fartings', 'bikes'))),
|
||||
);
|
||||
|
||||
$user = @$users[$this->get('id')];
|
||||
|
||||
if($user)
|
||||
{
|
||||
$this->response($user, 200); // 200 being the HTTP response code
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$this->response(array('error' => 'User could not be found'), 404);
|
||||
}
|
||||
}
|
||||
|
||||
function user_post()
|
||||
{
|
||||
//$this->some_model->updateUser( $this->get('id') );
|
||||
$message = array('id' => $this->get('id'), 'name' => $this->post('name'), 'email' => $this->post('email'), 'message' => 'ADDED!');
|
||||
|
||||
$this->response($message, 200); // 200 being the HTTP response code
|
||||
}
|
||||
|
||||
function user_delete()
|
||||
{
|
||||
//$this->some_model->deletesomething( $this->get('id') );
|
||||
$message = array('id' => $this->get('id'), 'message' => 'DELETED!');
|
||||
|
||||
$this->response($message, 200); // 200 being the HTTP response code
|
||||
}
|
||||
|
||||
function users_get()
|
||||
{
|
||||
//$users = $this->some_model->getSomething( $this->get('limit') );
|
||||
$users = array(
|
||||
array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
|
||||
array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com'),
|
||||
3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
|
||||
);
|
||||
|
||||
if($users)
|
||||
{
|
||||
$this->response($users, 200); // 200 being the HTTP response code
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$this->response(array('error' => 'Couldn\'t find any users!'), 404);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function send_post()
|
||||
{
|
||||
var_dump($this->request->body);
|
||||
}
|
||||
|
||||
|
||||
public function send_put()
|
||||
{
|
||||
var_dump($this->put('foo'));
|
||||
}
|
||||
}
|
251
application/controllers/api/key.php
Normal file
251
application/controllers/api/key.php
Normal file
@ -0,0 +1,251 @@
|
||||
<?php defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Keys Controller
|
||||
*
|
||||
* This is a basic Key Management REST controller to make and delete keys.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @subpackage Rest Server
|
||||
* @category Controller
|
||||
* @author Phil Sturgeon
|
||||
* @link http://philsturgeon.co.uk/code/
|
||||
*/
|
||||
|
||||
// This can be removed if you use __autoload() in config.php
|
||||
require(APPPATH.'/libraries/REST_Controller.php');
|
||||
|
||||
class Key extends REST_Controller
|
||||
{
|
||||
protected $methods = array(
|
||||
'index_put' => array('level' => 10, 'limit' => 10),
|
||||
'index_delete' => array('level' => 10),
|
||||
'level_post' => array('level' => 10),
|
||||
'regenerate_post' => array('level' => 10),
|
||||
);
|
||||
|
||||
/**
|
||||
* Key Create
|
||||
*
|
||||
* Insert a key into the database.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function index_put()
|
||||
{
|
||||
// Build a new key
|
||||
$key = self::_generate_key();
|
||||
|
||||
// If no key level provided, give them a rubbish one
|
||||
$level = $this->put('level') ? $this->put('level') : 1;
|
||||
$ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;
|
||||
|
||||
// Insert the new key
|
||||
if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits)))
|
||||
{
|
||||
$this->response(array('status' => 1, 'key' => $key), 201); // 201 = Created
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Key Delete
|
||||
*
|
||||
* Remove a key from the database to stop it working.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function index_delete()
|
||||
{
|
||||
$key = $this->delete('key');
|
||||
|
||||
// Does this key even exist?
|
||||
if ( ! self::_key_exists($key))
|
||||
{
|
||||
// NOOOOOOOOO!
|
||||
$this->response(array('status' => 0, 'error' => 'Invalid API Key.'), 400);
|
||||
}
|
||||
|
||||
// Kill it
|
||||
self::_delete_key($key);
|
||||
|
||||
// Tell em we killed it
|
||||
$this->response(array('status' => 1, 'success' => 'API Key was deleted.'), 200);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Update Key
|
||||
*
|
||||
* Change the level
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function level_post()
|
||||
{
|
||||
$key = $this->post('key');
|
||||
$new_level = $this->post('level');
|
||||
|
||||
// Does this key even exist?
|
||||
if ( ! self::_key_exists($key))
|
||||
{
|
||||
// NOOOOOOOOO!
|
||||
$this->response(array('error' => 'Invalid API Key.'), 400);
|
||||
}
|
||||
|
||||
// Update the key level
|
||||
if (self::_update_key($key, array('level' => $new_level)))
|
||||
{
|
||||
$this->response(array('status' => 1, 'success' => 'API Key was updated.'), 200); // 200 = OK
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$this->response(array('status' => 0, 'error' => 'Could not update the key level.'), 500); // 500 = Internal Server Error
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Update Key
|
||||
*
|
||||
* Change the level
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function suspend_post()
|
||||
{
|
||||
$key = $this->post('key');
|
||||
|
||||
// Does this key even exist?
|
||||
if ( ! self::_key_exists($key))
|
||||
{
|
||||
// NOOOOOOOOO!
|
||||
$this->response(array('error' => 'Invalid API Key.'), 400);
|
||||
}
|
||||
|
||||
// Update the key level
|
||||
if (self::_update_key($key, array('level' => 0)))
|
||||
{
|
||||
$this->response(array('status' => 1, 'success' => 'Key was suspended.'), 200); // 200 = OK
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$this->response(array('status' => 0, 'error' => 'Could not suspend the user.'), 500); // 500 = Internal Server Error
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Regenerate Key
|
||||
*
|
||||
* Remove a key from the database to stop it working.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function regenerate_post()
|
||||
{
|
||||
$old_key = $this->post('key');
|
||||
$key_details = self::_get_key($old_key);
|
||||
|
||||
// The key wasnt found
|
||||
if ( ! $key_details)
|
||||
{
|
||||
// NOOOOOOOOO!
|
||||
$this->response(array('status' => 0, 'error' => 'Invalid API Key.'), 400);
|
||||
}
|
||||
|
||||
// Build a new key
|
||||
$new_key = self::_generate_key();
|
||||
|
||||
// Insert the new key
|
||||
if (self::_insert_key($new_key, array('level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits)))
|
||||
{
|
||||
// Suspend old key
|
||||
self::_update_key($old_key, array('level' => 0));
|
||||
|
||||
$this->response(array('status' => 1, 'key' => $new_key), 201); // 201 = Created
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/* Helper Methods */
|
||||
|
||||
private function _generate_key()
|
||||
{
|
||||
//$this->load->helper('security');
|
||||
|
||||
do
|
||||
{
|
||||
$salt = do_hash(time().mt_rand());
|
||||
$new_key = substr($salt, 0, config_item('rest_key_length'));
|
||||
}
|
||||
|
||||
// Already in the DB? Fail. Try again
|
||||
while (self::_key_exists($new_key));
|
||||
|
||||
return $new_key;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/* Private Data Methods */
|
||||
|
||||
private function _get_key($key)
|
||||
{
|
||||
return $this->db->where('key', $key)->get(config_item('rest_keys_table'))->row();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
private function _key_exists($key)
|
||||
{
|
||||
return $this->db->where('key', $key)->count_all_results(config_item('rest_keys_table')) > 0;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
private function _insert_key($key, $data)
|
||||
{
|
||||
|
||||
$data['key'] = $key;
|
||||
$data['date_created'] = function_exists('now') ? now() : time();
|
||||
|
||||
return $this->db->set($data)->insert(config_item('rest_keys_table'));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
private function _update_key($key, $data)
|
||||
{
|
||||
return $this->db->where('key', $key)->update(config_item('rest_keys_table'), $data);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
private function _delete_key($key)
|
||||
{
|
||||
return $this->db->where('key', $key)->delete(config_item('rest_keys_table'));
|
||||
}
|
||||
}
|
14
application/controllers/barangaja.php
Normal file
14
application/controllers/barangaja.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
class Barangaja extends CI_Controller{
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
}
|
||||
function index(){
|
||||
$this->load->view('add');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
10
application/controllers/index.html
Normal file
10
application/controllers/index.html
Normal file
@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>403 Forbidden</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p>Directory access is forbidden.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
18
application/controllers/welcome.php
Normal file
18
application/controllers/welcome.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
class Welcome extends CI_Controller {
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
function index()
|
||||
{
|
||||
$this->load->helper('url');
|
||||
$this->load->view('welcome_message');
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file welcome.php */
|
||||
/* Location: ./system/application/controllers/welcome.php */
|
Reference in New Issue
Block a user