1.JPG

CodeIgniter這開放程式碼可參考[網頁技巧學習筆記]CodeIgniter套用,這裡會將CodeIgniter在XAMPP下執行,下載版本為CodeIgniter-3.1.6,將解完壓所檔放在路徑C:\xampp\htdocs,

9.JPG

在路徑C:\xampp\htdocs打開index.php

10.JPG

在第8行寫上要開啟資料夾名稱

header('Location: '.$uri.'/CodeIgniter-3.1.6/');

11.JPG

在XAMPP Control Panel開啟網頁,如下所示,

12.JPG

在XAMPP Control Panel開啟MySQL資料庫,新增資料庫 ci_database,

13.JPG

增加資料表message,

14.JPG

此要增加欄位如下所示,

15.JPG

16.JPG

在裡面新增一筆資料,指令如下,

INSERT INTO message (message) VALUES ("CI REST_Controller create success!!")

18.JPG

新增完成資料顯示如下,

19.JPG

以上完成後就來進行CodeIgniter資料庫設置,到路徑C:\xampp\htdocs\CodeIgniter-3.1.6\application\config

開啟database.php,如下所示,

9.JPG

下面是設置資料庫的各個項目,要了解各個項目可至這了解

https://www.codeigniter.com/userguide3/database/configuration.html

14.JPG

設置完成如下所示,

17.JPG

接下來到REST_Controller.php存放空間去下載,載點如下,

CodeIgniter Rest Server

紅色框為下載方式,

20.JPG

下載完成壓縮檔如下,

21.JPG

解完壓縮檔後

22.JPG

在路徑\codeigniter-restserver-master\application\libraries找到並複製REST_Controller.php,Format.php

23.JPG

複製到C:\xampp\htdocs\CodeIgniter-3.1.6\application\libraries中,

24.JPG

在路徑\codeigniter-restserver-master\application\language\english找到並複製rest_controller_lang.php

26.JPG

複製到C:\xampp\htdocs\CodeIgniter-3.1.6\application\language\english中,

25.JPG

在路徑\codeigniter-restserver-master\application\config找到並複製rest.php

1.PNG

複製到C:\xampp\htdocs\CodeIgniter-3.1.6\application\config中,

2.PNG

到路徑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

3.PNG

測試add_post

4.PNG

5.PNG

測試edit_post

6.PNG

7.PNG

測試remove_delete

8.PNG

9.PNG

 

arrow
arrow

    鄭智遠 發表在 痞客邦 留言(0) 人氣()