繁体   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