2021年2月22日 星期一

在docker-nginx-php-mysql開發環境,建立一個簡單的 PHP REST API (一)

 後篇:在docker-nginx-php-mysql開發環境,建立一個簡單的 PHP REST API (二)      

        看了資料來源01 ,竟然第一關就卡關。就開始找資料,希望可以進行實作。
        先來實作,再來處理理論的部分。那以影片1.How To Create A Simple REST API in PHP來實作。
         首先建立開發環境,以docker來做。使用資料依據為
一、shell檔案快速完成
檔名:DockerEnvInstall.sh
檔案內容:
#!/bin/bash
yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
yum install -y yum-utils
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
systemctl start docker
curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose version
yum install git
mkdir test01
cd test01
git clone https://github.com/mark2470mark/docker-nginx-php-mysql.git
cd ~/test01/docker-nginx-php-mysql
docker-compose up -d
docker-compose ps

重開機後,需要啟動docker,才能執行docker-compose up -d 等指令。
$sudo systemctl start docker

二、設定DockerEnvInstall.sh為可執行
$sudo chmod 755 DockerEnvInstall.sh
$sudo sh DockerEnvInstall.sh
此時的開發環境


三、在mysql建立資料庫,其資料庫名稱為 api 與建立資料表 users
     (1)建立資料庫 api
     (2)建立資料表 users
     (3)建立欄位id(int(10))、name(varchar(20))、age(int(2))
     (4)新增資料
四、建立php檔案
     (1)在~/test01/docker-ngnix-php-mysql/web/public/新增兩個檔案,分別是 config.php與index.php
           檔案名稱:index.php
           檔案內容:
            <?php
               require_once __DIR__.'/config.php';
                class API {
                  function  Select() {
                    $db = new Connect;
                    $users = array();
                    $data = $db->prepare('SELECT * FROM users ORDER BY id');
                    $data->execute();
                       while ($OutputData = $data->fetch(PDO::FETCH_ASSOC)) {
                                    $users[$OutputData['id']] = array(
                                     'id' =>  $OutputData['id'],
                                     'name' =>  $OutputData['name'],
                                      'age' =>  $OutputData['age']
                           );
                       }
                        return json_encode($users);
                     }
                    }
                   $API = new API;
                   header('Content-Type:application/json');
                   echo $API->Select();
                 ?>           
           檔案名稱:config.php
           檔案內容:
           <?php
              class Connect extends PDO {
                  public function __construct() {
                  parent::__construct("mysql:host=mysql;dbname=api",'root','a123456',
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
                  $this->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                  $this->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
                   }
                  }
                  ?>
五、結果呈現


沒有留言:

張貼留言

laravel 資料庫資料填充工廠入門

相關系列文章: 1. 在 windows 10 安裝 laravel 12 studentManagement環境與設定 2. laravel 12 route 路由 3. laravel 12 Blade Templates 網頁模版 4. laravel 12 Control...