CodeIgniter這開放程式碼可參考[網頁技巧學習筆記]CodeIgniter套用,這裡會將CodeIgniter在XAMPP下執行,下載版本為CodeIgniter-3.1.6,將解完壓所檔放在路徑C:\xampp\htdocs,
在路徑C:\xampp\htdocs打開index.php
在第8行寫上要開啟資料夾名稱
header('Location: '.$uri.'/CodeIgniter-3.1.6/');
在XAMPP Control Panel開啟網頁,如下所示,
在XAMPP Control Panel開啟MySQL資料庫,新增資料庫 ci_database,
增加資料表message,
此要增加欄位如下所示,
在裡面新增一筆資料,指令如下,
INSERT INTO message (message) VALUES ("CI REST_Controller create success!!")
新增完成資料顯示如下,
以上完成後就來進行CodeIgniter資料庫設置,到路徑C:\xampp\htdocs\CodeIgniter-3.1.6\application\config
開啟database.php,如下所示,
下面是設置資料庫的各個項目,要了解各個項目可至這了解
https://www.codeigniter.com/userguide3/database/configuration.html
設置完成如下所示,
接下來到REST_Controller.php存放空間去下載,載點如下,
紅色框為下載方式,
下載完成壓縮檔如下,
解完壓縮檔後
在路徑\codeigniter-restserver-master\application\libraries找到並複製REST_Controller.php,Format.php
複製到C:\xampp\htdocs\CodeIgniter-3.1.6\application\libraries中,
在路徑\codeigniter-restserver-master\application\language\english找到並複製rest_controller_lang.php
複製到C:\xampp\htdocs\CodeIgniter-3.1.6\application\language\english中,
在路徑\codeigniter-restserver-master\application\config找到並複製rest.php
複製到C:\xampp\htdocs\CodeIgniter-3.1.6\application\config中,
到路徑C:\xampp\htdocs\CodeIgniter-3.1.6\application\models,創建WelcomeModel.php,程式如下,
<?php
class WelcomeModel extends CI_Model {
function __construct() {
// Call the CI_Model constructor
parent::__construct();
// 載入database
$this->load->database();
}
public function getRow() {
$query = $this->db->get('message');
return $query->result();
}
public function insert($array) {
$this->db->insert('message', $array);
}
public function update($array) {
$this->db->update('message', $array, array('id' => $array['id']));
}
public function delete($array) {
$this->db->delete('message', $array);
}
}
到路徑C:\xampp\htdocs\CodeIgniter-3.1.6\application\controllers,修改Welcome.php,程式如下,
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use Restserver\Libraries\REST_Controller;
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';
class Welcome extends REST_Controller {
public function __construct() {
// Call the CI_Model constructor
parent::__construct();
$this->load->model('WelcomeModel');
$this->load->helper('url');
// 載入Date 輔助函式
$this->load->helper('date');
}
public function listpage_get() {
$data = $this->WelcomeModel->getRow();
return $this->set_response(['data' => $data, 'status' => true, 'message' => 'successful'], REST_Controller::HTTP_OK);
}
public function add_post() {
$array = array(
'message'=> $this->post('message')
);
$this->WelcomeModel->insert($array);
return $this->set_response(['status' => true, 'message' => 'successful'], REST_Controller::HTTP_OK);
}
public function edit_post() {
$array = array(
'id' => $this->post('id'),
'message'=> $this->post('message')
);
$this->WelcomeModel->update($array);
return $this->set_response(['status' => true, 'message' => 'successful'], REST_Controller::HTTP_OK);
}
public function remove_delete() {
$array = array(
'id' => $this->delete('id')
);
$this->WelcomeModel->delete($array);
return $this->set_response(['status' => true, 'message' => 'successful'], REST_Controller::HTTP_OK);
}
}
本人使用Chrome 應用程式 - Advanced REST client來做測試,
測試listpage_get
測試add_post
測試edit_post
測試remove_delete
留言列表