簡體   English   中英

如何在程序 PHP 之外構建 api 端點?

[英]How do you build api endpoints outside of procedural PHP?

I submitted a PHP Rest API project to a company, they sent a reply saying it was acceptable but they requested the removal of Procedural PHP code .

項目結構

This is the exact note they left "Procedural PHP code is allowed exclusively to initialize your PHP classes. Rest logic should be placed within class methods. - please, move product actions to the class/classes, do not use procedural PHP code. (create .php,刪除.php,並讀取.php文件)"

我不完全理解他們的意思,因為提到的 3 個文件是針對 API 調用的,我附上一張顯示項目結構的圖片,包括 3 個文件。 以讀取.php的代碼為例

<?php 
    //Headers
    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json');

    include_once '../../config/Database.php';
    include_once '../../Models/Product.php';

    //instantiate database
    $database = new Database();
    $db=$database->connect();
    //instantiate product object
    $product = new Product($db);
    
    //product query
    $result=$product->read();
    //get row number
    $num=$result->rowCount();

    //checkexistence of products
    if($num > 0){
        //product array
        $product_array= array();
        $product_array['data']= array();

        while($row = $result->fetch(PDO::FETCH_ASSOC)){
            extract($row);
            $product_item = array(
                'id'=>$id,
                'sku'=>$sku,
                'name'=>$name,
                'price'=>$price,
                'type'=>$type,
                'specs'=>$specs
            );
            //push to data
            array_push($product_array['data'],$product_item);
        }
        //turn to jsn
        echo json_encode($product_array);
    }else{
        
    }

知道它們是什么意思嗎? 以及如何解決它。

他們基本上是在要求您使用 OOP 做同樣的事情。 按照你的例子,你可以做這樣的事情

include_once '../../config/Database.php';
include_once '../../Models/Product.php';

class ProductApi
{
    protected $database;

    public function __construct()
    {
        $db = new Database();
        $this->database = $db->connect();
    }

    public function create()
    {
        // Api to create product
    }

    public function update()
    {
        // Api to update product
    }

    public function get()
    {
        $product = new Product($this->database);
        $result = $product->read();
        $num = $result->rowCount();
        if ($num > 0) {
            $product_array= array();
            $product_array['data'] = array();
            while($row = $result->fetch(PDO::FETCH_ASSOC)){
                extract($row);
                $product_item = array(
                    'id' => $id,
                    'sku' => $sku,
                    'name' => $name,
                    'price' => $price,
                    'type' => $type,
                    'specs' => $specs
                );
                array_push($product_array['data'], $product_item);
                header('Access-Control-Allow-Origin: *');
                header('Content-Type: application/json');
                echo json_encode($product_array);
                die;
            }
        } else {
            // No product found
        }
    }

    public function delete()
    {
        // Api to delete product
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM